我正在尝试实现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发布于 2015-02-05 10:22:20
以下代码就是问题所在:
#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();
}
#endregionhttps://stackoverflow.com/questions/28334926
复制相似问题