Description (2017-7-17-23:04:45):
Original:
The difference between overriding (override) and overwrite (new) in C #
Rewrite
The method of virtual modification with the keyword is called the virtual method. You can declare a method with the same name in the subclass with Override, which is called "overriding." The corresponding method is not modified by virtual, we call it a real method.
Rewriting alters the functionality of the parent class method.
Look at the following demo code:#region Rewrite
public class C1
{
Public virtual string GetName ()
{
Return "Uncle Xiang";
}
}
public class C2:c1
{
public override string GetName ()
{
return "Xiangshu";
}
}
C1 C1 = new C1 ();
Console.WriteLine (C1. GetName ());//output "Uncle Xiang"
C2 C2 = new C2 ();
Console.WriteLine (C2. GetName ());//Output "Xiangshu"
Here's the point!
C1 C3 = New C2 ();
Console.WriteLine (C3. GetName ());//Output "Xiangshu"
#endregion
cover
A method defined in a subclass with the same name as the parent class in the New keyword adornment is called overwrite.
Overwriting does not change the functionality of the parent class method. Look at the following demo code:#region Coverage
public class C1
{
public string GetName ()
{
Return "Uncle Xiang";
}
}
public class C2:c1
{
Public new string GetName ()
{
return "Xiangshu";
}
}
C1 C1 = new C1 ();
Console.WriteLine (C1. GetName ());//output "Uncle Xiang"
C2 C2 = new C2 ();
Console.WriteLine (C2. GetName ());//Output "Xiangshu"
Focus on this, and the above rewrite as a comparison
C1 C3 = New C2 ();
Console.WriteLine (C3. GetName ());//output "Uncle Xiang"
#endregion
Summarize
1: Neither rewriting nor overwriting will affect the functionality of the parent class itself (nonsense, yes, unless the code is changed).
2: When creating a parent class with a subclass, such as C1 C3 = New C2 (), overriding (override) alters the function of the parent class, that is, the function of the child class is called, while overwrite (new) does not, still calls the parent class feature.
3: Virtual methods, real methods can be overridden (new), abstract methods, interfaces are not available.
4: Abstract method, interface, method marked as virtual can be overridden (override), the real method is not possible.
5: The rewrite uses the frequency is relatively high, realizes the polymorphism, the coverage uses the frequency is relatively low, is used for inherits the class which previously cannot modify.
The difference between overriding (override) and overwrite (new) in C # learning note (--c#)