Possible repeat : Why is my virtual method not overestimated?
I am looking at First First Design templates. On the other hand, I started this book as a prerequisite for Code Complete 2. In any case, I work with the Decorator template (the chapter can even be read online) .
So, I have 4 classes:
- Beverage Class - Abstract
- Espresso Class - Inheritance of Beverages
- Dececorator Drink Class - Abstract
- Mocha Class - Inherits Decotrator for Beverages
Here is the source code for classes 1, 3, 4.
Beverage Class:
public abstract class Beverage { public Beverage() { Description = "Unknown Beverage"; } public String getDescription() { return Description; } public abstract double cost(); public String Description { get; set; } }
Beverage Class Decorator:
public abstract class BeverageDecorator : Beverage { public new abstract String getDescription(); }
Mocha Class:
public class Mocha : BeverageDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } public override string getDescription() { return beverage.Description + ", Mocha"; } public override double cost() { return beverage.cost() + .20; } }
So, they are pretty straightforward. Then, when I put this code in the Main () method, I continue to get the description "Unknown drink."
static void Main(string[] args) { Beverage beverage = new Espresso(); beverage = new Mocha(beverage); Console.WriteLine(beverage.Description + " $" + beverage.cost()); Console.Read(); }
Mocha is moving from the class he inherits - the Decorator for drinks - to a class above that - the drinks class. Although I have a method in Decorator for drinks and I redefine it. Why is this happening? I know this has something to do with abstract classes, but I just can't figure it out.
source share