【发布时间】:2016-11-03 11:31:09
【问题描述】:
对不起,如果这是一个愚蠢的问题或已经被问过;我试过用谷歌搜索,但我现在还不能真正测试这个......所以这里是:
想象一下这个场景:
public class Bottom
{
virtual public void foo()
{
doTheThing();
}
}
public class Middle : Bottom
{
virtual override public void foo() //this function will both override the parent's foo() and letting its child's foo() be over ridden
{
base.foo();
doTheOtherThing();
}
}
public class Top : Middle
{
override public void foo()
{
base.foo();
doTheFinalThing();
}
}
带有评论的中间那个是我要问的那个。
这样做可以吗?是传统的吗?关键字的顺序是否正确?有关系吗?这样做最正确的方法是什么?
【问题讨论】:
-
虚拟方法一直是虚拟的,你不需要为此做任何事情。所以只需从中间版本中删除“虚拟”关键字。
-
在你问之前,请尝试编译你感兴趣的代码。你的代码目前会给出一个编译时错误,所以这显然不是“一件好事”。
-
检查documentation:“您不能使用new、static或virtual修饰符来修改覆盖方法....被覆盖的属性必须是virtual、abstract或override。”跨度>
-
我现在明白了。谢谢大家——
标签: c# inheritance overriding virtual