Answer it.
We know the virtual method, and the overriding method.
However, it has not been found that the role of override and new is similar.
But why do we have to write two of them alone?
First of all, let's understand a problem where inheritance is linearly propagated.
classFather { Public intMoney = -; Public voidComany () = Console.WriteLine ("The company makes money every year!!! "); } classSon:father { PublicSon () = Console.WriteLine ($"I inherited my dad's {money}."); Public New voidComany () = Console.WriteLine ("I don't like this company."); }
In the code,son has a father method, a property. In other words, Son uses the Father method. On the son's inheritance chain, use new to hide the members/methods of the father. But it's just hiding. Does not mean that it does not exist.
Look at the example code:
New Son (). Comany (); // instantiate Son and use methods New Son (); // parent class is instantiated by subclasses Father.comany (); // Parent class Implementation method New Son (); // instantiating subclasses As Father). Comany (); // Strong-to-parent class, and implementation method
Results
In other words, the new keyword is simply a method of hiding in a subclass, which is simply that I do not want to use a method that uses the parent class in a subclass. However, methods that do not represent the parent class disappear. Subclasses still have a way to use the parent class
The simplest proof is to use base in a method to implement the method of the parent class using the New keyword. It still works.
Code:
.... Omitted
classSon:father { PublicSon () = Console.WriteLine ($"I inherited my dad's {money} and the company ."); Public New voidComany () =Base. Comany (); } Static voidMain (string[] args) { NewSon (). Comany ();//instantiate Son and use methodsFather Father=NewSon ();//parent class is instantiated by subclassesFather.comany ();//Parent class Implementation methodson son=NewSon ();//instantiating subclasses(son asFather). Comany ();//Strong-to-parent class, and implementation method }
What about override?
The override you can use at this stage can only be combined with the virtual keyword. So what's the override?
The relative new,override is completely covered. The method that packages the parent class.
Look at the code:
classProgram {Static voidMain (string[] args) { NewSon (). Comany ();//instantiate Son and use methodsFather Father=NewSon ();//parent class is instantiated by subclassesFather.comany ();//Parent class Implementation methodson son=NewSon ();//instantiating subclasses(son asFather). Comany ();//Strong-to-parent class, and implementation methodReadKey (); } } classFather { Public intMoney = -; Public Virtual voidComany () = Console.WriteLine ("The company makes money every year!!! "); } classSon:father { PublicSon () = Console.WriteLine ($"I inherited my dad's {money} and the company ."); Public Override voidComany () = Console.WriteLine ("I don't like this company."); }
Results:
See, even the father of the method has been to you the whole. As long as you are not simply using the parent class, it is impossible to implement the parent class's method through the subclass and override it.
Override is the existence of a method that completely kills virtual on the inheritance chain.
What if virtual and new are paired? Refer directly to the first method.
C + + polymorphism (2)