我有基类摘要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