C# 中“抽象覆盖”的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8905432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of 'abstract override' in C#?
提问by Manish Basantani
Just out of curiosity I tried overriding a abstract method in base class, and method the implementation abstract. As below:
出于好奇,我尝试覆盖基类中的抽象方法,并对实现抽象进行方法。如下:
public abstract class FirstAbstract
{
public abstract void SomeMethod();
}
public abstract class SecondAbstract : FirstAbstract
{
public abstract override void SomeMethod();
//?? what sense does this make? no implementaion would anyway force the derived classes to implement abstract method?
}
Curious to know why C# compiler allows writing 'abstract override'. Isn't it redundant? Should be a compile time error to do something like this. Does it serve to some use-case?
想知道为什么 C# 编译器允许编写“抽象覆盖”。不是多余的吗?做这样的事情应该是一个编译时错误。它是否适用于某些用例?
Thanks for your interest.
谢谢你的关注。
采纳答案by BrokenGlass
There's a useful example for this on Microsoft Docs- basically you can force a derived class to provide a new implementation for a method.
Microsoft Docs上有一个有用的示例- 基本上您可以强制派生类为方法提供新的实现。
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.
如果一个虚方法被声明为抽象的,它对于从抽象类继承的任何类来说仍然是虚的。继承抽象方法的类不能访问该方法的原始实现——在前面的例子中,类 F 上的 DoWork 不能调用类 D 上的 DoWork。这样,抽象类可以强制派生类为虚方法提供新的方法实现.
回答by Amar Palsapure
This is done because in child class you can not have abstractmethod with same name as in base class. overridetells compiler that you are overriding the behavior of base class.
这样做是因为在子类中你不能有abstract与基类中同名的方法。override告诉编译器您正在覆盖基类的行为。
Hope this is what you are looking for.
希望这是你正在寻找的。
回答by Jon
Imagine that SecondAbstractis in the middle of a three-class hierarchy, and it wants to implement some abstract methods from its base FirstAbstractwhile leaving some other method X to be implemented from its child ThirdAbstract.
想象一下,这SecondAbstract是在一个三类层次结构的中间,它想从它的基类实现一些抽象方法,FirstAbstract同时让一些其他方法 X 从它的 child 实现ThirdAbstract。
In this case, SecondAbstractis forced to decorate the method X with abstractsince it does not want to provide an implementation; at the same time, it is forced to decorate it with overridesince it is not defining a new method X, but wants to move the responsibility of implementing X to its child. Hence, abstract override.
在这种情况下,由于不想提供实现,SecondAbstract因此被迫用abstract它来装饰方法 X ;同时,它被迫装饰它,override因为它没有定义一个新的方法 X,而是想将实现 X 的责任转移到它的孩子身上。因此,abstract override。
In general, the concepts modelled by abstractand overrideare orthogonal. The first forces derived classes to implement a method, while the second recognizes that a method is the same as specified on a base class and not a newone.
通常,由abstract和建模的概念override是正交的。第一个强制派生类实现一个方法,而第二个认识到一个方法与基类上指定的相同,而不是new一个。
Therefore:
所以:
- neither keyword: "simple" method
abstractonly: derived class must implementoverrideonly: implementation of method defined in base classabstract override: derived class must implement a method defined in base class
- 关键字:“简单”方法
abstractonly:派生类必须实现overrideonly:基类中定义的方法的实现abstract override: 派生类必须实现基类中定义的方法
回答by Francois
If you did not declare SomeMethodas abstract overridein SecondAbstract, the compiler would expect that class to contain an implementation of the method. With abstract overrideit is clear that the implementation should be in a class derived from SecondAbstractand not in SecondAbstractitself.
如果您没有声明SomeMethod为abstract overridein SecondAbstract,编译器将期望该类包含该方法的实现。有了abstract override很明显的实现应该是从派生的类SecondAbstract,而不是在SecondAbstract本身。
Hope this helps...
希望这可以帮助...
回答by Eric Lippert
Interestingly enough, the Roslyn version of the C# compiler has an abstract override method in it, which I found odd enough to write an article about:
有趣的是,C# 编译器的 Roslyn 版本中有一个抽象的覆盖方法,我觉得很奇怪,我写了一篇关于:
回答by ghord
I find it really useful for ensuring proper ToString()implementation in derived classes. Let's say you have abstract base class, and you really want all derived classes to define meanigful ToString()implementation because you are actively using it. You can do it very elegantly with abstract override:
我发现它对于确保ToString()派生类中的正确实现非常有用。假设您有抽象基类,并且您确实希望所有派生类都定义有意义的ToString()实现,因为您正在积极使用它。您可以非常优雅地使用abstract override:
public abstract class Base
{
public abstract override string ToString();
}
It is a clear signal to implementers that ToString()will be used in base class in some way (like writing output to user). Normally, they would not think about defining this override.
对于ToString()将以某种方式在基类中使用的实现者来说,这是一个明确的信号(例如将输出写入用户)。通常,他们不会考虑定义此覆盖。
回答by noctonura
This design pattern is known as the Template Method pattern.
这种设计模式被称为模板方法模式。
Wikipedia page on Template Methods
A simple, non-software example: There are a bunch of military units: tanks, jets, soldiers, battleships, etc. They all need to implement some common methods but they will implement them very differently:
一个简单的非软件示例:有一堆军事单位:坦克、喷气式飞机、士兵、战舰等。它们都需要实现一些通用方法,但它们的实现方式却大不相同:
- Move()
- Attack()
- Retreat()
- Rest()
- 移动()
- 攻击()
- 撤退()
- 休息()
etc...
等等...

