关于c:使抽象的方法具有用于重写的主体

make abstract a method with body for overriding

我有我的饮料课,它有一些与饮料大小相关的getter/setter。这个程序与decorator模式有关,所以我想结合一些同样命名的方法的行为。

我的目的是要有一个身体的方法,允许我获得饮料的大小,但然后,我想能够覆盖儿童班的行为。

总之,我想要一种方法:

  • 如果不重写,则只表现为父类中的方法
  • 如果重写,则行为类似于对其进行编码

我所做的是创建一个名为getsizeofbeverage的方法,该方法的行为与我的"旧"getsize类似,并将getsizeofbeverage抽象为"新"方法,以便我可以重写它,但我需要一个不暗示新方法名的解决方案。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;

namespace Decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            Beverage beverage = new Espresso("small");
            beverage = new Whip(beverage);
            Console.WriteLine(beverage.getDescription() +" $" + beverage.cost());
        }
    }

    abstract class Beverage
    {
      private string description;
      private string size;

      public Beverage()
      {
        setDescription("Unknown beverage");
      }

      public double getCost()
      {
        return cost();
      }

      public abstract double cost();

      public abstract string getDescription();

      public void setDescription(string desc)
      {
        description = desc;
      }

      public string getSizeOfBeverage()
      {
        return size;
      }

      public abstract string getSize();

      public void setSize(string sz)
      {
        size = sz;
      }
    }

    class Espresso : Beverage
    {
      public Espresso(string sz)
      {
        setSize(sz);
        setDescription("Espresso");
      }

      public override double cost()
      {
        return 1.9;
      }

      public override string getDescription()
      {
        return getDescription();
      }

      public override string getSize()
      {
        return getSizeOfBeverage();
      }
    }

    abstract class CondimentDecorator : Beverage
    {
      public abstract override string getSize();
    }

    class Whip : CondimentDecorator
    {
      private Beverage beverage;

      public Whip(Beverage bv)
      {
        beverage = bv;
      }

      public override double cost()
      {
        if (getSize() =="small")
        {
          return 0.1 + beverage.cost();
        }
        else if (getSize() =="medium")
        {
          return 0.15 + beverage.cost();
        }
        else
        {
          return 0.2 + beverage.cost();
        }
      }

      public override string getDescription()
      {
        return beverage.getDescription() +", whip";
      }

      public override string getSize()
      {
        return beverage.getSizeOfBeverage();
      }
    }
}

if not overriden, just behaves as the method in the parent class if

overriden, behaves like it is coded

每个virtual方法的工作原理如下:

如果它被重写,它的行为将与被编码的行为类似,如果它不只是作为父类中的方法进行操作

从文档中获取虚拟

The virtual keyword is used to modify a method, property, indexer, or
event declaration and allow for it to be overridden in a derived
class. For example, this method can be overridden by any class that
inherits it:

抽象

When an instance method declaration includes an abstract modifier,
that method is said to be an abstract method. Although an abstract
method is implicitly also a virtual method, it cannot have the
modifier virtual. An abstract method declaration introduces a new
virtual method but does not provide an implementation of that method.
Instead, non-abstract derived classes are required to provide their
own implementation by overriding that method. Because an abstract
method provides no actual implementation, the method-body of an
abstract method simply consists of a semicolon.

在C中查看虚方法和抽象方法的区别#