【发布时间】:2015-02-05 01:56:49
【问题描述】:
我正在尝试实现装饰器模式,但是当我尝试编译我的程序时,我不断收到错误消息。我不知道为什么。我知道它与不是界面的东西有关,但是我尝试了很多更改,但没有任何效果。我感谢任何帮助!
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
【问题讨论】:
-
始终使用适当的语言标签标记您的问题。 (例如
c-sharp) -
谢谢。对于那个很抱歉。错误说:Error 1 'IShirtsComponent' in explicit interface declaration is not an interface'
标签: c# oop design-patterns visual-studio-2013 decorator