您可以覆盖私有虚拟方法吗?
问题描述:
我认为你可以,我的同事认为你不能!
I think you can and my colleage thinks you cannot!
答
你甚至不能声明私有虚拟方法.唯一有意义的时候是如果你有:
You can't even declare private virtual methods. The only time it would make any sense at all would be if you had:
public class Outer
{
private virtual void Foo() {}
public class Nested : Outer
{
private override void Foo() {}
}
}
...这是类型可以访问其父级私有成员的唯一情况.但是,这仍然是被禁止的:
... that's the only scenario in which a type has access to its parent's private members. However, this is still prohibited:
Test.cs(7,31):错误 CS0621:'Outer.Nested.Foo()': 虚拟或抽象成员不能是私有的
Test.cs(3,26): 错误 CS0621:'Outer.Foo()': 虚拟或抽象会员不能保持私密
Test.cs(7,31): error CS0621: 'Outer.Nested.Foo()': virtual or abstract members cannot be private
Test.cs(3,26): error CS0621: 'Outer.Foo()': virtual or abstract members cannot be private