使抽象方法具有覆盖的主体
make abstract a method with body for overriding
我有我的 Beverage class,它有一些 getters/setters 可以配合饮料的大小。这个程序与装饰器模式有关,所以我想结合一些同名方法的行为。
我的意图是有一个方法 body 允许我获取饮料的大小,但是,我希望能够在 child classes 上覆盖该行为。
总之,我想要一个方法:
- 如果不被重写,则与 parent class
中的方法一样
- 如果被覆盖,行为就像它被编码一样
我所做的是创建一个名为 getSizeOfBeverage 的方法,其行为与我的 "old" getSize 所做的一样,并使getSize "new" 方法抽象因此我可以覆盖它,但我想要一个不暗示新方法名称的解决方案。
这是我的代码:
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
方法都是这样工作的:
如果它被重写,它将表现得像编码的那样,如果不只是表现得像父方法中的方法class
来自 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.
看virtual and abstract methods in C#
的区别
我有我的 Beverage class,它有一些 getters/setters 可以配合饮料的大小。这个程序与装饰器模式有关,所以我想结合一些同名方法的行为。
我的意图是有一个方法 body 允许我获取饮料的大小,但是,我希望能够在 child classes 上覆盖该行为。
总之,我想要一个方法:
- 如果不被重写,则与 parent class 中的方法一样
- 如果被覆盖,行为就像它被编码一样
我所做的是创建一个名为 getSizeOfBeverage 的方法,其行为与我的 "old" getSize 所做的一样,并使getSize "new" 方法抽象因此我可以覆盖它,但我想要一个不暗示新方法名称的解决方案。
这是我的代码:
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
方法都是这样工作的:
如果它被重写,它将表现得像编码的那样,如果不只是表现得像父方法中的方法class
来自 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.
看virtual and abstract methods in C#
的区别