1: Method overrides: The method in the base class is identified with the virtual keyword and then overridden in the inheriting class
(override), so that the methods in the base class have been overridden in the subclass, and the methods in the base class have lost functionality in the subclass
The When a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method is a heavy
Write the method.
2: Method hiding: The New keyword can be used in an inheriting class, regardless of whether the method in the base class uses the virtual keyword (if the new
does not produce an error, but generates a compilation warning) hides the methods in the base class so that hidden is hidden, not as heavy
Write, overriding is that the old method in the base class already does not exist in the subclass, while hiding is the method in the base class and the overridden in the subclass of the new
Methods are present. So when a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method invokes the
The method of the base class.
classBaseClass { Public voidFunctiona () {Console.WriteLine ("Basefunctiona = = ="); } Public Virtual voidfunctionb () {Console.WriteLine ("Basefunctionb = = ="); } } classDerivedclass:baseclass {New Public voidFunctiona () {Console.WriteLine ("Derivedfunctiona = = ="); } Public Override voidfunctionb () {Console.WriteLine ("Derivedfunctionb = = ="); } } classProgram {Static voidMain (string[] args) {BaseClass B2=NewDerivedClass (); B2.functiona ();//Basefunctiona = = =B2.functionb ();//Derivedfunctionb = = =Console.WriteLine ("-----"); BaseClass b=NewBaseClass (); B.functiona ();//Basefunctiona = = =B.functionb ();//Basefunctionb = = =Console.WriteLine ("-----"); DerivedClass d=NewDerivedClass (); D.functiona ();//Derivedfunctiona = = =D.functionb ();//Derivedfunctionb = = =Console.WriteLine ("-----"); } }
Source: http://blog.51cto.com/745559847/1857264
[Go] C # hiding methods and overriding methods