首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >装饰器模式: WIll未运行

装饰器模式: WIll未运行
EN

Stack Overflow用户
提问于 2015-02-05 09:56:50
回答 1查看 76关注 0票数 0

我正在尝试实现Decorator模式,但是当我试图编译我的程序时,我总是得到一个错误。我想不出为什么。我知道这与不是一个接口有关,但我已经尝试了一大堆更改,但都不起作用。非常感谢您的帮助!

代码语言:javascript
复制
          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
EN

回答 1

Stack Overflow用户

发布于 2015-02-05 10:22:20

以下代码就是问题所在:

代码语言:javascript
复制
    #region ShirtsComponent Members

    string ShirtsComponent.GetBrand()
    {
        return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand);
    }

    double ShirtsComponent.GetPrice()
    {
        return _price + fashion_Base.GetPrice();
    }
    #endregion

您不需要在方法声明中指定基类。这是正确的代码:

代码语言:javascript
复制
    #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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28334926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档