在派生类成员使用具有与基类成员同名的新关键字的好处 [英] benefit of using new keyword in derived class member having same name with base class member

查看:140
本文介绍了在派生类成员使用具有与基类成员同名的新关键字的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#语言规范说,如果我继承一个类,基类和派生类中具有相同签名的同名成员,那么我必须使用关键字隐藏基类成员(目前使用的基础虚拟和override关键字和派生类成员的另一种方式)。

The C# language specification says that if I inherit a class, and the base class and derived class have the same named member with the same signature, then I have to use the new keyword to hide the base class member (There is another way by using virtual and override keyword in base and derived class member).

但在实践中我发现,派生类自动隐藏衍生成员,如果它具有相同的命名成员。那么,什么是相同的命名派生类成员的主要效益与问题新的关键字?

But in practice I found that the derived class auto hides the derived member if it has the same named member. So what is the main benefit and problem new keyword in same named derived class member?

推荐答案

新未的需要的,因为你注意到。它的可选的,如果你不使用它,你就会得到一个警告。你是完全正确的注意,这是第一眼一个奇怪的设计决策。

New is not required, as you note. It is optional, and if you do not use it, you get a warning. You are entirely correct to note that this is at first glance a strange design decision.

这个设计决定的目的是帮助减轻一类问题被称为脆基类问题。下面是问题的一个版本:

The purpose of this design decision is to help mitigate a class of problems known as the "Brittle Base Class" problems. Here's a version of that problem:

美孚公司创建类Frobber和船舶它Foo.DLL 1.0版:

Foo Corporation creates a class Frobber and ships it in Foo.DLL version 1.0:

namespace FooCorp
{
  public class Frobber
  {
    public void Frobnicate() { ... }
    ...

酒吧公司,你工作谁,让Blobbers。一个Blobber可以做的一切,一个Frobber可以做,但除此之外,它可以Blobnicate了。所以你决定重新使用Frobnicate从FooCorp的实施,并增加了部分附加功能:

Bar Corporation, who you work for, makes Blobbers. A Blobber can do everything that a Frobber can do, but in addition, it can Blobnicate too. So you decide to re-use the implementation of Frobnicate from FooCorp, and add some additional functionality:

namespace BarCorp
{
  public class Blobber : FooCorp.Frobber
  {
    public void Blobnicate() { ... }
    ...

美孚公司意识到人喜欢Blobnicate,他们决定出货Foo.DLL V2.0:

Foo Corporation realizes that people like to Blobnicate, and they decide to ship Foo.DLL v2.0:

namespace FooCorp
{
  public class Frobber
  {
    public void Frobnicate() { ... }
    public void Blobnicate() { ... }
    ...

当你得到一个新的Foo.DLL的版本并重新编译,您想告诉你现在意外引进了隐藏了基类方法的新方法这可能是做一件危险的事情。类与基类是Frobnicator假设写的,但显然现在是一个Blobnicator呢!这一事实可以打破的的客户,谁可能会意外地调用基类的版本时,他们打算打电话给你的派生类的版本。

When you get a new version of Foo.DLL and recompile, you want to be told that you are now accidentally introducing a new method that shadows a base class method. That is possibly a dangerous thing to do; your class was written with the assumption that the base class was a Frobnicator, but apparently now it is a Blobnicator too! That fact could break your customers, who might accidentally call the base class version when they intended to call your derived class version.

我们做新的可选的,所以它的法律的为你的影子在不改变你的源代码的基类方法。如果我们是非法那么的 FooCorp将其升级打破了你的身材。的但是我们使它成为一个警告,这样的你知道,你可能会这么不小心的做。然后,您可以仔细检查代码;如果你决定,你Blobnicate的实现是多余的,现在,你可以将其删除。如果还是不错的,可以将其标记为新,消除警告。

We make "new" optional so that it is legal for you to shadow a base class method without changing your source code. If we made it illegal then FooCorp would have broken your build with their upgrade. But we make it a warning so that you know that you might be doing so accidentally. You can then examine the code carefully; if you decide that your implementation of Blobnicate is now redundant, you can remove it. If it is still good, you can mark it as "new" and eliminate the warning.

有意义吗?这是C#的微妙的功能,使其适合大型多版本的面向组件的的软件之一。

Make sense? This is one of the subtle features of C# that make it suitable for large scale multi-version component-oriented software.

这篇关于在派生类成员使用具有与基类成员同名的新关键字的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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