覆盖多级继承中的方法(Overriding Methods in Multi

编程入门 行业动态 更新时间:2024-10-23 18:31:15
覆盖多级继承中的方法(Overriding Methods in Multi-level Inheritence)

给定三个父母/孩子班级,像这样:

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.

更多推荐

本文发布于:2023-07-04 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Overriding   Multi   Methods

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!