我有基類摘要Goods
類並繼承了Book
類。抽象類字段冗餘C#
abstract class Goods
{
public decimal weight;
string Title, BarCode;
double Price;
public Goods(string title, string barCode, double price)
{
Title = title;
BarCode = barCode;
Price = price;
}
}
abstract class Book : Goods
{
protected int NumPages;
public Book(string title, string barCode, double price, int numPages)
: base(title, barCode, price)
{
NumPages = numPages;
weight = 1;
}
public override void display()
{
base.display();
Console.WriteLine("Page Numbers:{0}", NumPages);
}
}
我應該寫title
,barCode
,price
存在於Goods
類兩次?我可以取代這
public Book(string title, string barCode, double price, int numPages)
: base(title, barCode, price)
與較少的重複建設?
只是一個說明。你有**字段**而不是**屬性**。屬性應該有'get'和/或'set'。 –
請使用'decimal'(或整數)而不是'double'來表示價格。 – CodesInChaos
我會考慮用接口來替換抽象類。 – CodesInChaos