这是我的代码:
class StockItem
{
internal float CostPrice;
internal string Description;
internal static int LastStockNumber = 10000;
internal int StockNumber;
public StockItem(int StockNumber, string Description, float CostPrice): this(Description, CostPrice)
{
this.StockNumber = StockNumber;
}
public StockItem(string Description, float CostPrice)
{
LastStockNumber++;
this.StockNumber = LastStockNumber;
this.CostPrice = CostPrice;
this.Description = Description;
}
public float GetCostPrice()
{
return CostPrice;
}
public virtual string Print() //is virtual (Polymorphic)
{
string Output = "";
Output += "\r\n\r\n";
Output += "Stock Item: ";
Output += "\r\n";
Output += "Stock No: " + StockNumber;
Output += "\r\n";
Output += "Desc: " + Description;
Output += "\r\n";
Output += "Cost: " + CostPrice;
Output += "\r\n";
return Output;
}
}
}
class HeavyStockItem : StockItem
{
internal float Weight;
public HeavyStockItem(int StockNumber, string Description, float CostPrice, float Weight)
: base(StockNumber, Description, CostPrice)
{
this.Weight = Weight;
}
public HeavyStockItem(string Description, float CostPrice, float Weight)
: base(Description, CostPrice)
{
LastStockNumber++;
this.StockNumber = LastStockNumber;
this.Weight = Weight;
}
public float GetWeight()
{
return Weight;
}
public override String Print() //overriding StockItem.Print and adds wieght to the bottom
{
string Output = "";
Output += base.Print();
Output += "Weight: " + Weight + "\r\n";
return Output;
}
}
}
class CarEngine : HeavyStockItem
{
internal string EngineNumber;
public CarEngine(int StockNumber, string Description, float CostPrice, float Weight, string EngineNumber)
: base(StockNumber, Description, CostPrice, Weight)
{
this.EngineNumber = EngineNumber;
}
public CarEngine(string Description, float CostPrice, float Weight, string EngineNumber)
: base(Description, CostPrice, Weight)
{
LastStockNumber++;
this.StockNumber = LastStockNumber;
}
public override String Print() //overriding StockItem.Print and adds engine number to the bottom
{
string Output = "";
Output += base.Print();
Output += "EngineNumber: " + EngineNumber + "\r\n";
return Output;
}
}
}
public partial class Form1 : Form
{
StockItem StockItem1;
CarEngine StockItem2;
CarEngine StockItem3;
StockItem StockItem4;
HeavyStockItem StockItem5;
private void ShowItem (StockItem PrintStockItem)
{
txtOutput.Text += PrintStockItem.Print();
}
private void Form1_Load(object sender, EventArgs e)
{
StockItem1 = new StockItem(StockItem.LastStockNumber, "Scrediwer set", 42);
StockItem2 = new CarEngine(8025, "MazdaB6T", 1252, 800, "Z4537298D");
StockItem3 = new CarEngine(StockItem.LastStockNumber, "Holden 308", 958, 1104, "P74623854S");
StockItem4 = new StockItem(8002, "Trolley Jack", 127);
StockItem5 = new HeavyStockItem(HeavyStockItem.LastStockNumber, "JD Caterpillar Track", 3820, 2830);
}
private void btnList_Click(object sender, EventArgs e) //polymorfic call
{
ShowItem(StockItem1);
ShowItem(StockItem2);
ShowItem(StockItem3);
ShowItem(StockItem4);
ShowItem(StockItem5);
}
}
}
这是我的输出:
Stock Item:
Stock No: 10000
Desc: Scrediwer set
Cost: 42
Stock Item:
Stock No: 8025
Desc: MazdaB6T
Cost: 1252
Weight: 800
EngineNumber: Z4537298D
Stock Item:
Stock No: 10002
Desc: Holden 308
Cost: 958
Weight: 1104
EngineNumber: P74623854S
Stock Item:
Stock No: 8002
Desc: Trolley Jack
Cost: 127
Stock Item:
Stock No: 10004
Desc: JD Caterpillar Track
Cost: 3820
Weight: 2830
我的问题是:
而不是让项目 1,3 和 5 的库存编号为 10000、10001 和 10002,我按照上述方式获得。无法理解为什么?