如果我不注意警告“隐藏继承的成员”怎么办?使当前成员覆盖该实现......”
这可能是一个很好的点,但它涉及到编译器在您执行以下操作时发出的警告:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它不会做任何事情,但 F() 中的方法也不是多态的。如果您开始使用基类引用,那么代码中很容易出错,因此您会收到警告。当然它可以是你想要的,因此 new 关键字只是让它更明确,
例如
现在,如果您使用
new
添加类C
,它的行为将与B
相同。如果您使用override
添加类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.
Now, if you add class
C
withnew
, it will behave the same asB
. If you add classD
withoverride
, thenF
becomes polymorphic:See this fiddle.
不会,除了是否发出警告之外,默认行为与使用
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 specifyoverride
instead ofnew
, 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.
这只是警告。代码会起作用。在某些情况下,此警告可能会导致一些问题。
将 new 关键字与您从基类调用的方法一起使用。
参考
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.
Reference
https://msdn.microsoft.com/en-us/library/435f1dw2.aspx
https://msdn.microsoft.com/en-us/library/aa691135(v=vs.71).aspx