我正在尝试实现Decorator模式,但是当我尝试编译程序时,我一直收到错误。我无法弄清楚为什么。我知道它与某些东西不是一个接口,但我已经尝试了一系列的改变,没有任何工作。我感谢任何帮助!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OODAssignment3_ZackDavidson
{
class Program
{
static void Main(string[] args)
{
//Creates a new Calvin Klein Shirt fordecoration
CalvinKlein ckShirt = new CalvinKlein();
Console.WriteLine(Convert.ToString(ckShirt.GetBrand()));
//Puts a solid color on the Calvin Klein Shirt
solidColorDecorator ckSCD = new solidColorDecorator(ckShirt);
//Puts stripes on the Calvin Klein Shirt
stripedShirtDecorator ckSSD = new stripedShirtDecorator(ckShirt);
//Puts a pocket on the Clavin Klein Shirt
pocketShirtDecorator ckPSD = new pocketShirtDecorator(ckShirt);
//Creates a new Tommy Hilfiger Shirt
TommyHilfiger thShirt = new TommyHilfiger();
//Puts stripes on the Tommy Hilfiger Shirt
stripedShirtDecorator thSSD = new stripedShirtDecorator(thShirt);
//Puts a pocket on the Tommy Hilfiger Shirt
pocketShirtDecorator thPSD = new pocketShirtDecorator(thShirt);
}//EndOfMain
}//EndOfClassProgram
public abstract class ShirtsComponent
{
public abstract string GetBrand();
public abstract double GetPrice();
}
class CalvinKlein : ShirtsComponent
{
private string ck_Brand = "Calvin Klein";
private double ck_Price = 75.0;
public override string GetBrand()
{
return ck_Brand;
}
public override double GetPrice()
{
return ck_Price;
}
}
class TommyHilfiger : ShirtsComponent
{
private string th_Brand = "Tommy Hilfiger";
private double th_price = 85.0;
public override string GetBrand()
{
return th_Brand;
}
public override double GetPrice()
{
return th_price;
}
}
public abstract class Decorator : ShirtsComponent
{
ShirtsComponent fashion_Base = null;
protected string _brand = "Undefined Decorator";
protected double _price = 0.0;
protected Decorator(ShirtsComponent fashionBase)
{
fashion_Base = fashionBase;
}
#region ShirtsComponent Members
string ShirtsComponent.GetBrand()
{
return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand);
}
double ShirtsComponent.GetPrice()
{
return _price + fashion_Base.GetPrice();
}
#endregion
}
class solidColorDecorator : Decorator
{
public solidColorDecorator(ShirtsComponent fashionBase)
: base(fashionBase)
{
this._brand = "Solid Color Shirt";
this._price = 25.0;
}
}
class stripedShirtDecorator : Decorator
{
public stripedShirtDecorator(ShirtsComponent fashionBase)
: base(fashionBase)
{
this._brand = "Striped Shirt";
this._price = 50.0;
}
}
class pocketShirtDecorator : Decorator
{
public pocketShirtDecorator(ShirtsComponent fashionBase)
: base(fashionBase)
{
this._brand = "Dotted Shirt";
this._price = 90.0;
}
}
}//EndOfNamespace
答案 0 :(得分:1)
这段代码就是问题:
#region ShirtsComponent Members
string ShirtsComponent.GetBrand()
{
return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand);
}
double ShirtsComponent.GetPrice()
{
return _price + fashion_Base.GetPrice();
}
#endregion
您不需要在方法声明中指定基类。这是正确的代码:
#region ShirtsComponent Members
public override string GetBrand()
{
return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand);
}
public override double GetPrice()
{
return _price + fashion_Base.GetPrice();
}
#endregion