gpt4 book ai didi

c# - 使抽象方法具有覆盖的主体

转载 作者:行者123 更新时间:2023-11-30 19:37:13 24 4
gpt4 key购买 nike

我有我的Beverage 类,它有一些 getter/setter 来处理饮料的大小。这个程序与装饰器模式有关,所以我想结合一些同名方法的行为。

我的意图是拥有一个方法体,允许我获取饮料的大小,但随后,我希望能够在子类上覆盖该行为。

总而言之,我想要一个方法:

  • 如果没有被重写,就如同父类中的方法一样
  • 如果被重写,则表现得像被编码一样

我所做的是创建一个名为 getSizeOfBeverage 的方法,它的行为类似于我的“旧”getSize 所做的,并使 getSize 成为“新的” "方法抽象所以我可以覆盖它,但我想要一个不暗示新方法名称的解决方案。

这是我的代码:

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 方法都是这样工作的:

如果它被重写,它将表现得像它被编码的那样,如果不只是表现得像父类中的方法一样

来自 virtual 的文档

The virtual keyword is used to modify a method, property, indexer, orevent declaration and allow for it to be overridden in a derivedclass. For example, this method can be overridden by any class thatinherits it:

abstract

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

查看 virtual and abstract methods in C# 之间的区别

关于c# - 使抽象方法具有覆盖的主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com