c# - make abstract a method with body for overriding -
i have beverage class, has getters/setters work size of beverage. program has decorator pattern, want combine behavior of methods equally named.
my intent have method body allows me size of beverage, then, want able override behavior on child classes.
in sum, want method that:
- if not overriden, behaves method in parent class
- if overriden, behaves coded
what did create method called getsizeofbeverage behaves "old" getsize did, , made getsize "new" method abstract can override it, want solution not imply new method name.
here code:
using system; namespace decorator { class program { static void main(string[] args) { beverage beverage = new espresso("small"); beverage = new whip(beverage); console.writeline(beverage.getdescription() + " $" + beverage.cost()); } } abstract class beverage { private string description; private string size; public beverage() { setdescription("unknown beverage"); } public double getcost() { return cost(); } public abstract double cost(); public abstract string getdescription(); public void setdescription(string desc) { description = desc; } public string getsizeofbeverage() { return size; } public abstract string getsize(); public void setsize(string sz) { size = sz; } } class espresso : beverage { public espresso(string sz) { setsize(sz); setdescription("espresso"); } public override double cost() { return 1.9; } public override string getdescription() { return getdescription(); } public override string getsize() { return getsizeofbeverage(); } } abstract class condimentdecorator : beverage { public abstract override string getsize(); } class whip : condimentdecorator { private beverage beverage; public whip(beverage bv) { beverage = bv; } public override double cost() { if (getsize() == "small") { return 0.1 + beverage.cost(); } else if (getsize() == "medium") { return 0.15 + beverage.cost(); } else { return 0.2 + beverage.cost(); } } public override string getdescription() { return beverage.getdescription() + ", whip"; } public override string getsize() { return beverage.getsizeofbeverage(); } } }
if not overriden, behaves method in parent class if
overriden, behaves coded
every virtual
method works this:
if overriden behave coded, if not behaves method in parent class
from documentation virtual
the virtual keyword used modify method, property, indexer, or event declaration , allow overridden in derived class. example, method can overridden class inherits it:
and abstract
when instance method declaration includes abstract modifier, method said abstract method. although abstract method implicitly virtual method, cannot have modifier virtual. abstract method declaration introduces new virtual method not provide implementation of method. instead, non-abstract derived classes required provide own implementation overriding method. because abstract method provides no actual implementation, method-body of abstract method consists of semicolon.
see difference between virtual , abstract methods in c#
Comments
Post a Comment