当前位置:编程学习 > C#/ASP.NET >>

装饰模式问题

public abstract class Beverage
    {
        public string description;
        public 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.0;
        }
    }
public class Milk:CondimentDecorator
    {
        private Beverage beverage;
        public Milk(Beverage beverage)
        {
            this.beverage = beverage;
        }
        public override string getDescription()
        {
            return beverage.getDescription() + ",Milk";
        }
        public override double cost()
        {
            return beverage.cost() + 0.3;
        }
    }
 public class Mocha:CondimentDecorator
    {
        private Beverage beverage;
        public Mocha(Beverage beverage)
        {
            this.beverage = beverage;
        }
        public override string getDescription()
        {
            return beverage.getDescription() + ",Mocha";
        }
        public override double cost()
        {
            return beverage.cost() + 0.2;
        }
    }
class Program
    {
        static void Main(string[] args)
        {            
            Beverage beverage = new DarkRoast();
            Mocha mocha=new Mocha(beverage);
            Milk milk=new Milk(mocha);
            Console.WriteLine(milk.getDescription() + "$" + milk.cost());
            Console.ReadKey();
        }
    }
正确的结果应该为DarkRoast,Milk,Mocha $2.5 大神求助装饰模式 --------------------编程问答-------------------- 不看了,照猫画虎吧 --------------------编程问答-------------------- 无法调用到Mocha重写的那个getDescription();直接调用基类的getDescription();所以结果就成了,Milk$2.5了我想问一下在CondimentDecorator类中已经覆盖了基类Beverage中的getDescription()而Mocha中重写类该方法怎么还会去调基类的方法? --------------------编程问答-------------------- 因为你传进去的是Beverage,他会先转换成Beverage在调用getDescription()由于Mocha继承了CondimentDecorator而没有继承Beverage所以出现了上面的结果。
--------------------编程问答-------------------- 看错了,由于抽象类Beverage中的非抽象方法没有加virtual关键字,因此他的子类默认覆盖(new)而不是重写(override)他的方法。 --------------------编程问答-------------------- public abstract class Beverage
    {
        public string description;
        public virtual string getDescription()
        {
            return description;
        }
        public abstract double cost();
    }
public class DarkRoast:Beverage
    {
        public DarkRoast()
        {
            description = "DarkRoast";
        }
        public override double cost()
        {
            return 2.0;
        }
    }
public class Milk : Beverage
    {
        private Beverage beverage;
        public Milk(Beverage beverage)
        {
            this.beverage = beverage;
        }
        public override string getDescription()
        {
            return beverage.getDescription() + ",Milk";
        }
        public override double cost()
        {
            return beverage.cost() + 0.3;
        }
    }
public class Mocha : Beverage
    {
        private Beverage beverage;
        public Mocha(Beverage beverage)
        {
            this.beverage = beverage;
        }
        public override string getDescription()
        {
            return beverage.getDescription() + ",Mocha";
        }
        public override double cost()
        {
            return beverage.cost() + 0.2;
        }
    }
class Program
    {
        static void Main(string[] args)
        {            
            Beverage beverage = new DarkRoast();
            Mocha mocha=new Mocha(beverage);
            Milk milk=new Milk(mocha);
            Console.WriteLine(milk.getDescription() + "$" + milk.cost());
            Console.ReadKey();
        }
    } --------------------编程问答-------------------- CondimentDecorator已经继承了Beverage,所以milk也是Beverage的子类吧? --------------------编程问答-------------------- CondimentDecorator已经继承了Beverage,所以milk也是Beverage的子类吧? --------------------编程问答-------------------- 是呀,但是是覆盖不是重写呀,所以。
另外抽象方法重写虚方法也有问题,我下面给你回的应该是对的吧。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,