如果我不注意警告“隐藏继承的成员”怎么办?使当前成员覆盖该实现......”

发布于 2024-11-05 20:28:02 字数 474 浏览 1 评论 0原文

这可能是一个很好的点,但它涉及到编译器在您执行以下操作时发出的警告:

class A
{
    public virtual void F() { }
}
class B : A
{
    public void F() { }
}

然后您会收到警告:

'EomApp1.BF()'隐藏继承的成员'EomApp1.AF()'。< br> 要使当前成员覆盖该实现,请添加 override 关键字。否则请使用 new 关键字。

错误消息的图像

问题:如果出现以下情况,实际警告我会发生什么警告:我对此什么都不做?如果添加“new”关键字与不添加“new”关键字,我的程序的功能是否会有所不同?

(注意:我知道我可以很容易地测试这个,但我认为值得在这里询问)

This is maybe a fine point, but it concerns the warning that the compiler issues if you do something like:

class A
{
    public virtual void F() { }
}
class B : A
{
    public void F() { }
}

Then you get the warning:

'EomApp1.B.F()' hides inherited member 'EomApp1.A.F()'.
To make the current member override that implementation, add the override keyword. Otherwise use the new keyword.

image of error message

QUESTION: What is the warning actually warning me will happen if I do nothing about it? Will my program function differently if I add the 'new' keyword vs. if I do not?

(Note: I know I could test this easily enough but I figured it was worth asking here)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡笑忘祈一世凡恋 2024-11-12 20:28:02

它不会做任何事情,但 F() 中的方法也不是多态的。如果您开始使用基类引用,那么代码中很容易出错,因此您会收到警告。当然它可以是你想要的,因此 new 关键字只是让它更明确,

例如

B b1 = new B();
b1.F(); // will call B.F()

A b2 = new B();
b2.F(); // will call A.F()

现在,如果您使用 new 添加类 C,它的行为将与 B 相同。如果您使用 override 添加类 D,则 F 将变为多态:

class C : A
{
    public new void F() { }
}

class D : A
{
    public override void F() { }
}

// Later

C c1 = new C();
c1.F(); // will call C.F()

A c2 = new C();
c2.F(); // will call A.F()

D d1 = new D();
d1.F(); // will call D.F()

A d2 = new D();
d2.F(); // will call D.F()

请参阅 这个小提琴

It will not do anything but the method you have there F() is also not polymorphic. It becomes very easy to make mistake in code if you started to use base class reference hence you are warned. Of course it can be what you want, hence the new keyword just make it more explicit

E.g.

B b1 = new B();
b1.F(); // will call B.F()

A b2 = new B();
b2.F(); // will call A.F()

Now, if you add class C with new, it will behave the same as B. If you add class D with override, then F becomes polymorphic:

class C : A
{
    public new void F() { }
}

class D : A
{
    public override void F() { }
}

// Later

C c1 = new C();
c1.F(); // will call C.F()

A c2 = new C();
c2.F(); // will call A.F()

D d1 = new D();
d1.F(); // will call D.F()

A d2 = new D();
d2.F(); // will call D.F()

See this fiddle.

¢好甜 2024-11-12 20:28:02

不会,除了是否发出警告之外,默认行为与使用 new 时的行为完全相同。根据我的经验,您通常实际上想要指定override而不是new,因为这通常只是忘记显式覆盖的情况。

如果您真的确实想要一个完全独立的方法,并且如果您可以选择使用不同的名称,那么我会采用这种方法。在我看来,用一种方法隐藏另一种方法通常会显着降低可读性。如果目的只是提供更具体的返回类型(并且确保更弱类型的方法返回相同的值),这也不算太糟糕,但这种情况非常罕见。

No, the default behaviour is exactly the same as if you use new, other than whether the warning is given. In my experience you more often actually want to specify override instead of new, as it's usually just a case of forgetting to explicitly override.

If you really do want a completely separate method, and if you've got the option of using a different name instead, I'd take that approach. Having one method hiding another usually reduces readability significantly IMO. It's not too bad if the purpose is to just give a more specific return type (and you make sure the more weakly typed method returns the same value), but that's a pretty rare case.

终弃我 2024-11-12 20:28:02

这只是警告。代码会起作用。在某些情况下,此警告可能会导致一些问题。

new 关键字与您从基类调用的方法一起使用。

class A
{
    public virtual void F() { }
}
class B : A
{
   new public void F() { }
}

参考

https://msdn.microsoft.com/en-us/library/435f1dw2。 aspx

https://msdn.microsoft .com/en-us/library/aa691135(v=vs.71).aspx

This is just warning. Code will work. In some circumstance, this warning can lead to some problem.

use new keyword with the method which you are calling from the base class.

class A
{
    public virtual void F() { }
}
class B : A
{
   new public void F() { }
}

Reference

https://msdn.microsoft.com/en-us/library/435f1dw2.aspx

https://msdn.microsoft.com/en-us/library/aa691135(v=vs.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文