我有一个基础抽象类Goods
,并继承了Book
类。
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);
}
}
我需要在Goods
类中写两次title
、barCode
和price
吗?还是说我可以替换它们?
public Book(string title, string barCode, double price, int numPages)
: base(title, barCode, price)
有没有更少冗余的结构?
get
和/或set
方法。 - Dovydas Šopa