您可以覆盖私有虚拟方法吗? [英] Can you override private virtual methods?

查看:27
本文介绍了您可以覆盖私有虚拟方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为你可以,我的同事认为你不能!

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

这篇关于您可以覆盖私有虚拟方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆