我刚刚接触到 C# 的世界。我现在正在学习如何在 Windows 窗体中使用抽象类和虚拟方法。我有结构/想法,但需要帮助填写我的表单代码。我的基本抽象类名为Plants
,四个子类名为Trees
、和Tomatoes
,一个名为 的虚拟方法和一个在所有方法之外的名为的方法。 Seeds
Berries
Get_value
Report
我想button1
单击为每个类创建至少两个对象,并使用对另一个类的textBox1
调用将它们显示在多行中,以显示每种植物的价值和所有植物的总价值。关于如何解决这个问题的任何想法或帮助?Report
textBox2
代码
namespace plant_farm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public abstract class Plants
{
protected string the_name;
//may need more strings but not sure
public virtual string Get_Value()
{
return "";
/*
Multiplies the number of items times the value per
item and returns the product as a double.
*/
}
public override string ToString()
{
return "";
/*
Returns a string that gives all the relevant information
for the object(including an indication of wheter it is a
tree, seed, etc.
*/
}
}
public class Trees : Plants
{
/*
Includes a variety(oak, cherry, etc.), height in feet,
the number of trees in stock, and the price of each tree.
*/
}
public class Tomatoes : Plants
{
/*
Includes: Type(big boy, early girl, etc.), the size of the
tomatoes expected (small, medium, or large), the price of each plat,
the number of plants per plat (6, 12, 24, etc.) and the number of
plats in stock.
*/
}
public class Seeds : Plants
{
/*
Includes: Type of seeds(pumpkin, cantaloupe, cucumber, etc.),
the number of packets in stock, and the cost per packet.
*/
}
public class Berries : Plants
{
/*
Includes: Type of plant(blackberry, strawberry, etc.), the variety
(AAA early, FrostStar, etc), the month of highest bearing (May, June,
etc.), the number of plants in stock, and the price per plant.
*/
}
public void Report()
{
/*
Is passed a Plant object and a TextBox and adds the ToString result of
the object to the textbox and then skips to the next line.
*/
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}