我有以下代码,工作正常:
public abstract class Beverage
{
public string description = "Unknown beverage";
public virtual string getDescription()
{
return description;
}
public abstract double cost();
}
public abstract class condimentDecorator : Beverage
{
// public abstract string getDescription();
}
public class DarkRoast : Beverage
{
public DarkRoast()
{
description = "DarkRoast";
}
public override double cost()
{
return 2.10;
}
}
public class Espresso : Beverage
{
public Espresso()
{
description = "Espresso";
}
public override double cost()
{
return 1.99;
}
}
public class HouseBlend : Beverage
{
public HouseBlend()
{
description = "House Blend Coffee";
}
public override double cost()
{
return .89;
}
}
public class Mocha : condimentDecorator
{
Beverage beverage;
public Mocha(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Mocha";
}
public override double cost()
{
return .20 + beverage.cost();
}
}
public class Soy : condimentDecorator
{
Beverage beverage;
public Soy(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Soy";
}
public override double cost()
{
return .10 + beverage.cost();
}
}
public class Whip : condimentDecorator
{
Beverage beverage;
public Whip(Beverage beverage)
{
this.beverage = beverage;
}
public override string getDescription()
{
return beverage.getDescription() + ", Whip";
}
public override double cost()
{
return .10 + beverage.cost();
}
}
我正在以这种方式使用它:
protected void Page_Load(object sender, EventArgs e)
{
Beverage beverage2 = new DarkRoast();
beverage2 = new Mocha(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
Response.Write ("<br> " + beverage2.getDescription() + " : $" + beverage2.cost().ToString());
}
问题:我希望所有子类“condimentDecorator”强制覆盖getDescription()函数,因为我在“condimentDecorator”类中编写了以下代码:
public abstract string getDescription();
但是这改变了我当前的功能并且没有给出期望的结果它只显示“未知饮料”作为getDescription()的值,它是父类最多的类的值。
正常结果:
DarkRoast, Mocha, Mocha, Whip : $2.6
使用“public abstract string getDescription()”后的结果:
Unknown beverage : $2.6
请建议我应该写什么/更改,以便我可以强制“condimentDecorator”的子类覆盖“getDescription();”并且在没有它的情况下也可以重新开始工作。
答案 0 :(得分:3)
我认为您的类层次结构可以使用一些重新思考。
这个怎么样:
我的建议是你创建一个界面IBeverage
,你可以在所有可饮用的东西上实现它。然后你为“基本”饮料创建一个基类 - DarkRoast,Espresso,HouseBlend - 就像你现在一样。
对于Condiments,您实现了一个实现IBeverage的新抽象基类,但没有提供默认的GetDescription
实现。这个类也可以在其构造函数中使用IBeverage来强制其他调味品也这样做。
我认为这样的事情应该有用(未经测试,未编译 - 但你明白了)
public interface IBeverage
{
string GetDescription ();
}
public abstract class BeverageBase : IBeverage
{
public virtual string GetDescription () { return "Unknown Beverage"; }
}
public class DarkRoast : BeverageBase { ... }
public class HouseBlend : BeverageBase { ...}
public abstract class CondimentBase : IBeverage
{
public CondimentBase(IBeverage beverage)
{
Beverage = beverage;
}
protected IBeverage Beverage { get; set; }
public abstract string GetDescription ();
}
public class Mocha : CondimentBase
{
public Mocha(IBeverage beverage)
: base (beverage)
{ }
public string GetDescription()
{
return Beverage.GetDescription() + ", Mocha";
}
}
答案 1 :(得分:1)
快速谷歌证明了这一点: http://codebetter.com/blogs/darrell.norton/archive/2005/05/18/63332.aspx
我没有尝试过代码。
答案 2 :(得分:0)
你可以像这样创建一个新方法:
public abstract class condimentDecorator : Beverage
{
public override string getDescription()
{
return getDescriptionInternal();
}
protected abstract string getDescriptionInternal();
}
这样,所有类都必须间接实现getDescription。