cannot declare a body because it is marked abstract
嗨,我是C控制台应用程序的新成员,我正在使用抽象和重写,但是我在公共抽象double compute()的第一个方法中得到了堆栈。我得到了一个错误,它说不能声明一个主体,因为它被标记为抽象。请帮助我。谢谢您!
`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | abstract class Cake { public string _flavor, _size; public int _quantity; public Cake(string flavor, string size, int quantity) { _flavor = flavor; _size = size; _quantity = quantity; } public abstract double Compute() { double price; if(_flavor =="Chocolate" && _size =="Regular") { price = 250.50; } else if (_flavor =="Chocolate" && _size =="Large") { price = 450.50; } else if (_flavor =="Strawberry" && _size =="Regular") { price = 300.50; } else { price = 500.75; } return price; } } class BirthdayCake:Cake { public int _numOfCandles; public BirthdayCake(string flavor, string size, int quantity, int numOfCandles):base(flavor,size,quantity) { _numOfCandles = numOfCandles; } public override double Compute() { return _numOfCandles * 10.00; } }` |
如果有默认实现,但希望允许子类重写,请使用虚拟而不是抽象
如前所述,你不能为
您需要创建从您的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | abstract class Cake { public string _flavor, _size; public int _quantity; abstract public double Compute(); } class BirthdayCake : Cake { public int _numOfCandles; public BirthdayCake(string flavor, string size, int quantity, int numOfCandles):base(flavor,size,quantity) { _numOfCandles = numOfCandles; } public override double Compute() { //does your stuff } } |