Home  >  Article  >  Backend Development  >  Versioning with the Override and New Keywords (C# Programming Guide)

Versioning with the Override and New Keywords (C# Programming Guide)

黄舟
黄舟Original
2017-02-16 10:58:171534browse

Original address: Click to open the link



##This has Many meanings. This means, for example, that introducing a new member in a base class with the same name as a member in a derived class is fully supported in C# and will not lead to unexpected behavior. It also means that the class must explicitly declare whether a method is to override an inherited method or a new method that hides an inherited method with a similar name.

In C#, a derived class can contain methods with the same names as base class methods.

  • #The base class method must be defined as virtual.

  • If a method in a derived class is not preceded by the new or override keyword, the compiler will issue a warning and the method will appear as if it exists new keyword performs the same operations.

  • If a method in a derived class is preceded by the new keyword, the method is defined as independent of the method in the base class .

  • If a method in a derived class is preceded by the override keyword, the object of the derived class will call the method instead of calling Base class methods.

  • You can use the base keyword to call base class methods from a derived class.

  • ##override, virtual and new keywords can also be used for attributes and indexes devices and events.

If a method is declared virtual, any class that inherits the method can implement its own version. To make a method a virtual method, you must use the virtual modifier in the method declaration of the base class. Then, the derived class can use the override keyword to override the base virtual method, or use the new keyword to hide the virtual method in the base class. If neither the override keyword nor the new keyword is specified, the compiler will issue a warning and methods in the derived class will hide methods in the base class.

GraphicsClass is as follows:

C



class GraphicsClass
{    public virtual void DrawLine() { }    public virtual void DrawPoint() { }
}


Your company uses this class, and you Use this to derive your own class when adding new methods:

C



class YourDerivedGraphicsClass : GraphicsClass
{    public void DrawRectangle() { }
}


Your application runs fine until Company A releases a new version of GraphicsClass, similar to the following code:

C




class GraphicsClass
{    public virtual void DrawLine() { }    public virtual void DrawPoint() { }    public virtual void DrawRectangle() { }
}


At the beginning, no problems occurred. The new version remains binary compatible with the old version. Any software that has been deployed will continue to work normally even if new classes are installed on the computer system where the software resides. In your derived class, any existing calls to method DrawRectangle will continue to reference your version.

This warning indicates that you must consider how you want the DrawRectangle method to work in your application.

If you want your method to override the new base class method, please use the override keyword:

C



#
class YourDerivedGraphicsClass : GraphicsClass
{    public override void DrawRectangle() { }
}


 派生自 YourDerivedGraphicsClass 的对象仍可以使用基关键字访问DrawRectangle 的基类版本

C#


base.DrawRectangle();


 为了避免这两个方法之间发生混淆,可以重命名您的方法。 这可能很耗费时间且容易出错,而且在某些情况下并不可行。 但是,如果您的项目相对较小,则可以使用 Visual Studio 的重构选项来重命名方法。 有关更多信息,请参见重构类和类型(类设计器)。

或者,也可以通过在派生类定义中使用关键字 new 来防止出现该警告:

C#


class YourDerivedGraphicsClass : GraphicsClass
{    public new void DrawRectangle() { }
}


 这是默认行为。

重写和方法选择

 下面的方法将是兼容的:

C#


public class Derived : Base
{    public override void DoWork(int param) { }    public void DoWork(double param) { }
}


 重写方法不被视为是在类上进行声明的,而是在基类上声明的方法的新实现。 仅当 C# 编译器无法将方法调用与 Derived 上的原始方法匹配时,它才尝试将该调用与具有相同名称和兼容参数的重写方法匹配。 例如:

C#



int val = 5;
Derived d = new Derived();
d.DoWork(val);  // Calls DoWork(double).


 有两种方法可以避免此情况。 首先,避免将新方法声明为与虚方法同名。 其次,可以通过将 Derived 的实例强制转换为 Base 来使 C# 编译器搜索基类方法列表,从而使其调用虚方法。 由于是虚方法,因此将调用 Derived 上的 DoWork(int) 的实现。 例如:

C#


((Base)d).DoWork(val);  // Calls DoWork(int) on Derived.


有关 new 和 override的更多示例,请参见 了解何时使用 Override 和 New 关键字(C# 编程指南)。

 以上就是使用 Override 和 New 关键字进行版本控制(C# 编程指南)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn