为什么我们需要新的关键字,为什么是默认的行为隐瞒不重写? [英] Why do we need the new keyword and why is the default behavior to hide and not override?

查看:86
本文介绍了为什么我们需要新的关键字,为什么是默认的行为隐瞒不重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个<一个href=\"http://geekswithblogs.net/BlackRabbit$c$cr/archive/2010/06/24/c-fundamentals-beware-of-implicit-hiding.aspx\">blog帖子并以下几个问题:

I was looking at this blog post and had following questions:


  • 我们为什么需要关键字,它只是规定一个基类方法被隐藏。我的意思是,我们为什么需要它?如果我们不使用覆盖关键词,我们是不是隐藏基类的方法?

  • 为什么在C#中默认为隐藏和不覆盖?为什么设计者实现这种方式?

  • Why do we need the new keyword, is it just to specify that a base class method is being hidden. I mean, why do we need it? If we don't use the override keyword, aren't we hiding the base class method?
  • Why is the default in C# to hide and not override? Why have the designers implemented it this way?

推荐答案

好的问题。让我再次声明他们。

Good questions. Let me re-state them.

为什么合法的隐藏方法,在所有其他方法?

Why is it legal to hide a method with another method at all?

我来回答一个例子这个问题。你有一个接口,从CLR V1:

Let me answer that question with an example. You have an interface from CLR v1:

interface IEnumerable
{
    IEnumerator GetEnumerator();
}

超。现在,在CLR V2你有仿制药,你认为男人,要是我们在V1有仿制药,我会做这个的通用接口。但我没有。我应该让兼容的东西与它现在的的通用的,所以我得到仿制药的好处,又不失向后兼容code,它预计IEnumerable的兼容性。

Super. Now in CLR v2 you have generics and you think "man, if only we'd had generics in v1 I would have made this a generic interface. But I didn't. I should make something compatible with it now that is generic so that I get the benefits of generics without losing backwards compatibility with code that expects IEnumerable."

interface IEnumerable<T> : IEnumerable
{
    IEnumerator<T> .... uh oh

你有什么打算叫的IEnumerable&LT的GetEnumerator方法; T&GT; ?记住,你的希望的可隐藏的GetEnumerator非通用底座接口上。您的从不的希望被称为那个东西,除非你在一个向后compat的情况是明确的。

What are you going to call the GetEnumerator method of IEnumerable<T>? Remember, you want it to hide GetEnumerator on the non-generic base interface. You never want that thing to be called unless you're explicitly in a backwards-compat situation.

这仅仅证明方法躲藏。有关方法隐藏的理由更多的心思看关于这个问题我的文章

That alone justifies method hiding. For more thoughts on justifications of method hiding see my article on the subject.

为什么不躲新导致警告?

Why does hiding without "new" cause a warning?

由于我们希望把它带到你的注意力,你有难言之隐,可能会被意外做。的记住,你可能,而不是由你编辑派生类中隐藏着什么意外,因为编辑由别人完成的基类。

Because we want to bring it to your attention that you are hiding something and might be doing it accidentally. Remember, you might be hiding something accidentally because of an edit to the base class done by someone else, rather than by you editing your derived class.

为什么不躲新的警告,而不是错误?

Why is hiding without "new" a warning rather than an error?

同样的道理。你可能会隐藏着什么意外,因为你刚刚拿起一个基类的新版本。这一切发生的时间。 FooCorp使一个基类B. BarCorp使用的方法吧派生类D,因为他们的客户喜欢这种方法。 FooCorp看到这些说嘿,这是一个好主意,我们可以把这些功能的基类。他们这样做和船舶Foo.DLL的新版本,当BarCorp拿起新的版本,这将是很好,如果他们被告知,他们的方法现在隐藏基类的方法。

Same reason. You might be hiding something accidentally because you've just picked up a new version of a base class. This happens all the time. FooCorp makes a base class B. BarCorp makes a derived class D with a method Bar, because their customers like that method. FooCorp sees that and says hey, that's a good idea, we can put that functionality on the base class. They do so and ship a new version of Foo.DLL, and when BarCorp picks up the new version, it would be nice if they were told that their method now hides the base class method.

我们希望这种情况是一个的警告的,而不是一个的错误的原因使它成为一个错误意味着的这是脆基类问题的另一种形式的。 C#经过精心设计,以便当有人提出了改变一个基类,在code使用派生类的影响降到最小。

We want that situation to be a warning and not an error because making it an error means that this is another form of the brittle base class problem. C# has been carefully designed so that when someone makes a change to a base class, the effects on code that uses a derived class are minimized.

为什么隐藏,而不是覆盖默认?

Why is hiding and not overriding the default?

由于虚拟倍率的危险的。虚拟覆盖允许派生类改变code被编译为使用基类的行为。做一些危险的喜欢做一个覆盖应该是你做的东西的自觉地有意的,不是偶然的。

Because virtual override is dangerous. Virtual override allows derived classes to change the behaviour of code that was compiled to use base classes. Doing something dangerous like making an override should be something you do consciously and deliberately, not by accident.

这篇关于为什么我们需要新的关键字,为什么是默认的行为隐瞒不重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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