C#继承不像我期望的那样

时间:2016-07-29 20:55:24

标签: c# inheritance decorator

我正在经历头脑设计模式,但我在C#而不是java中编写示例。我编码装饰模式,但我得到一些奇怪的错误,我无法弄清楚如何解决。 第1行按预期运行,描述为“Dark Roast”,但第二行将描述更改为“未知饮料”而不是预期的“Dark Roast,Mocha”。我该如何解决这个问题?

主程序:

beverage = new DarkRoast();
beverage = new Mocha(beverage);
beverage = new Mocha(beverage);
Console.WriteLine(beverage.Description + " $" + beverage.Cost);

装饰员代码:

namespace DecoratorPattern
{
    public abstract class Beverage
    {
        public abstract double Cost { get; }

        public string Description { get; set; } = "unknown beverage";
    }

    public class DarkRoast : Beverage
    {
        public DarkRoast()
        {
            Description = "Dark Roast";
        }

        public override double Cost => .99;
    }

    public abstract class CondimentDecorator : Beverage
    {
        public abstract string Description { get; }
    }

    public class Mocha : CondimentDecorator
    {
        Beverage beverage;

        public Mocha(Beverage beverage)
        {
            this.beverage = beverage;
        }

        public override string Description
        {
            get { return beverage.Description + ", Mocha"; }
        }

        public override double Cost => .20 + beverage.Cost;
    }
}

2 个答案:

答案 0 :(得分:3)

您尚未在 #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSArray *baseArray; @property (nonatomic, strong) NSMutableArray *datesArray; @property (nonatomic, strong) NSMutableArray *countArray; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Base Array of counts should be - 07/1 (4), 7/2 (3), 7/3 (2), 7/4 (1) self.baseArray = @[@"07/01/16",@"07/01/16",@"07/01/16",@"07/01/16", @"07/02/16",@"07/02/16",@"07/02/16", @"07/03/16",@"07/03/16", @"07/04/16"]; self.datesArray = [[NSMutableArray alloc]init]; [self.datesArray addObjectsFromArray:self.baseArray]; [self.datesArray addObject:[NSNull null]]; self.countArray = [[NSMutableArray alloc]init]; int marker = 0; int counter = 0; //For loop for count of elements for (int i = 0; i < self.datesArray.count; i++) { if (self.datesArray[marker] == self.datesArray[i]) { counter++; NSLog(@"MDate %@ Index %@ Counter: %d Marker: %d", self.datesArray[marker], self.datesArray[i], counter, marker); } else if (self.datesArray[marker] != self.datesArray[i]){ [self.countArray addObject:[NSNumber numberWithInt:counter]]; NSLog(@"Count is: %d", counter); marker = i; --i; counter = 0; } } NSLog(@"Count Array is: %@", self.countArray); } @end 课程中将Description属性标记为virtual。因此,如果您的变量Beveragebeverage,则始终会从那里读取Beverage属性。该值始终是默认的未知值。

只需从Description类中删除abstract声明并将CondimentDecorator添加到基类,然后您的覆盖就可以了。

答案 1 :(得分:1)

如果您将类型更改为autorelease,则会在Description中隐藏原始CondimentDecorator媒体资源,并在其上方生成新的媒体资源Description然后你会得到预期的结果。

这应打印您期望的值:

CondimentDecorator

您需要将基础更改为抽象或虚拟,并在继承类

上覆盖它
 var result = (CondimentDecorator) beverage;
 Console.WriteLine(result.Description);

此时你不需要额外的装饰器类只是覆盖基础对象中的方法

public abstract class Beverage
{
    public abstract double Cost { get; }

    public virtual Description { get; set; } = "unknown beverage";
}