Rewrite
The virtual method is called the virtual method. You can use override to declare a method with the same name in the subclass. This is called "Override ". The corresponding method without virtual modification is called its real method.
Rewriting changes the function of the parent class method.
See the following demo Code : # Region Rewrite
Public class C1
{< br> Public virtual string getname ()
{< br> return " Xu mingxiang " ;< BR >}
Public class C2: c1
{< br> Public override string getname ()
{< br> return " xumingxiang " ;< BR >}
C1 C1 =NewC1 ();
Console. writeline (c1.getname ());//Output "Xu mingxiang"
C2 C2 =NewC2 ();
Console. writeline (c2.getname ());//Output "xumingxiang"
//Focus here
C1 C3 =NewC2 ();
Console. writeline (c3.getname ());//Output "xumingxiang"
# Endregion
Overwrite
In the subclass, The New Keyword is used to modify the method defined with the same name as the parent class, which is called overwrite.
Overwrite Does not change the function of the parent class method.
See the following DEMO code:# RegionOverwrite
Public class C1
{< br> Public string getname ()
{< br> return " Xu mingxiang " ;
}< BR >}
Public class C2: c1
{< br> Public New string getname ()
{< br> return " xumingxiang " ;< BR >}
C1 C1 =NewC1 ();
Console. writeline (c1.getname ());//Output "Xu mingxiang"
C2 C2 =NewC2 ();
Console. writeline (c2.getname ());//Output "xumingxiang"
//Here, we will compare it with the above rewriting.
C1 C3 =NewC2 ();
Console. writeline (c3.getname ());//Output "Xu mingxiang"
# Endregion
Summary
1: Neither rewriting nor overwriting will affect the features of the parent class (nonsense, certainly, unless the code is changed ).
2: When a child class is used to create a parent class, such as C1 C3 =NewC2 (), rewriting will change the function of the parent class, that is, calling the function of the subclass; but overwriting will not, still calling the function of the parent class.
3: Virtual Methods and real methods can be overwritten (new), Abstract methods, and interfaces cannot.
4: Abstract METHODS, interfaces, and Methods marked as virtual can be overwritten. Real methods are not allowed.
5: The frequency of rewriting is relatively high, achieving polymorphism; the frequency of overwriting is relatively low, used to inherit classes that cannot be modified before.
original address: http://www.cnblogs.com/xumingxiang/archive/2012/04/14/override_new.html Author: Xu mingxiang
Source: http://www.cnblogs.com/xumingxiang
copyright: The copyright of this article belongs to the author and blog park a total of
reprint: Welcome to reprint, to save the author's Creative Enthusiasm, please [repost] As required. Thank you
requirement: This statement must be retained without the author's consent; the original connection must be provided in Article ; otherwise, the legal liability is required.