Differences between new, override, and virtual keywords in C #
The oo idea has been widely used in software development projects. The most important feature is inheritance. Recently, I briefly reviewed the inheritance feature in C, the keywords that need to be used, including some key points, are specially sorted out for your convenience.
I. in C #, the keyword "new" is frequently used and has three main functions:
A)It is used as an operator to create an object and call constructor.
B)As a modifier.
C)It is used to restrict the types of parameters that may be used as type parameters in a generic declaration.
In this article, we will only introduce the function of new as a modifier. When used as a modifier, The New Keyword can hide the method of the base class in the derived class, that is to say, the method used in the derived class is to call the method with the new keyword.New definitionInstead of the base class method. If you do not use the new keyword to hide the base class method, the compiler will receive a warning prompting you to use the new keyword to hide the base class method.
Note that the new and override keywords are mutually exclusive. It cannot be used at the same time.
II. The main keyword of override isProvides a new implementation of the derived class to the base class method.The override base class method must be the same as the override method.Same signatureThis keyword cannot be used to override non-virtual and static methods. The keywords used with this keyword are virtual, abstract, and override. At the same time, the override method cannot modify the accessibility of the virtual method. The override method and the virtual method must have the same access modifier, you cannot use modifiers new, static, virtual, or abstract to modify the override method.
The following is a small demo that shows the essential differences between new and override:
Class Program
{
Static VoidMain (String[] ARGs)
{
ContactCT1 =New Class2();
ContactCT2 =New Class2();
Ct1.prinf ();
Ct2.prinf ();
}
}
Abstract Public Class Contact
{
Public Virtual VoidPrinf ()
{
Console. Writeline ("This is a virtual Method");
}
}
Public Class Class1:Contact
{
Public Override VoidPrinf ()
{
Console. Writeline ("This is a new method.");
}
}
Public Class Class2:Contact
{
Public New VoidPrinf ()
{
Console. Writeline ("This is another new method.");
}
}
The running result of this demo is:
This is a new method.
This is a virtual Method
III,Virtual Keywords allow re-writing these objects in a derived class. By default, methods are non-virtual and non-virtual methods cannot be rewritten. Virtual keywords cannot be used together with static, abstract, private, and override. The virtual keyword is closely related to override. To implement the virtual method, you must use the override or New Keyword (new and override generate different mechanisms ).
Conclusion: The New Keyword is mainly used to distinguish between a derived class and a method with the same name as the base class. By hiding the base class method, the compiler can call the correct method. Override is mainly used
And virtual methods.
From: http://www.cnblogs.com/grokyao/archive/2007/07/31/837814.html
The oo idea has been widely used in software development projects. The most important feature is inheritance. Recently, I briefly reviewed the inheritance feature in C, the keywords that need to be used, including some key points, are specially sorted out for your convenience.
I. in C #, the keyword "new" is frequently used and has three main functions:
A)It is used as an operator to create an object and call constructor.
B)As a modifier.
C)It is used to restrict the types of parameters that may be used as type parameters in a generic declaration.
In this article, we will only introduce the function of new as a modifier. When used as a modifier, The New Keyword can hide the method of the base class in the derived class, that is to say, the method used in the derived class is to call the method with the new keyword.New definitionInstead of the base class method. If you do not use the new keyword to hide the base class method, the compiler will receive a warning prompting you to use the new keyword to hide the base class method.
Note that the new and override keywords are mutually exclusive. It cannot be used at the same time.
II. The main keyword of override isProvides a new implementation of the derived class to the base class method.The override base class method must be the same as the override method.Same signatureThis keyword cannot be used to override non-virtual and static methods. The keywords used with this keyword are virtual, abstract, and override. At the same time, the override method cannot modify the accessibility of the virtual method. The override method and the virtual method must have the same access modifier, you cannot use modifiers new, static, virtual, or abstract to modify the override method.
The following is a small demo that shows the essential differences between new and override:
Class Program
{
Static VoidMain (String[] ARGs)
{
ContactCT1 =New Class2();
ContactCT2 =New Class2();
Ct1.prinf ();
Ct2.prinf ();
}
}
Abstract Public Class Contact
{
Public Virtual VoidPrinf ()
{
Console. Writeline ("This is a virtual Method");
}
}
Public Class Class1:Contact
{
Public Override VoidPrinf ()
{
Console. Writeline ("This is a new method.");
}
}
Public Class Class2:Contact
{
Public New VoidPrinf ()
{
Console. Writeline ("This is another new method.");
}
}
The running result of this demo is:
This is a new method.
This is a virtual Method
III,Virtual Keywords allow re-writing these objects in a derived class. By default, methods are non-virtual and non-virtual methods cannot be rewritten. Virtual keywords cannot be used together with static, abstract, private, and override. The virtual keyword is closely related to override. To implement the virtual method, you must use the override or New Keyword (new and override generate different mechanisms ).
Conclusion: The New Keyword is mainly used to distinguish between a derived class and a method with the same name as the base class. By hiding the base class method, the compiler can call the correct method. Override is mainly used
And virtual methods.
From: http://www.cnblogs.com/grokyao/archive/2007/07/31/837814.html