给定三个父母/孩子班级,像这样:
class A { public virtual void doSomething() { //do things } } class B : A { public override /*virtual?*/ void doSomething() { //do things base.doSomething(); } } class C : B { public override void doSomething() { //do things base.doSomething(); } }B类的doSomething()方法在它的签名中是否有override和virtual ,因为它也被C类覆盖,或者只有类A在它的doSomething()方法签名中有virtual ?
Given three parent/child classes, like this:
class A { public virtual void doSomething() { //do things } } class B : A { public override /*virtual?*/ void doSomething() { //do things base.doSomething(); } } class C : B { public override void doSomething() { //do things base.doSomething(); } }Should class B's doSomething() method have both override and virtual in its signature, since it also is overridden by the C class, or should only class A have virtual in its doSomething() method signature?
最满意答案
如果某个方法已经在其中一个超类中标记为虚拟,则不需要(读取:不能)将方法标记为虚拟方法。
该方法在整个继承树中保持虚拟,直到子类将其标记为sealed 。 密封的方法不能被任何子类覆盖。
You don't need to (read: you can't) mark a method as virtual, if it has already been marked as virtual in one of the super classes.
The method will remain virtual throughout the inheritance tree until a subclass marks it as sealed. A sealed method cannot then be overridden by any of the subclasses.
更多推荐
发布评论