usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacenange_1{classA { Public voidF () {Console.WriteLine ("in the base class"); } } classb:a { Public voidF () {Console.WriteLine ("in the child class"); } } classProgram {Static voidMain (string[] args) {B OBJB=NewB (); A Obja=OBJB; The //base class points to the derived class object obja.f (); //But the method of the base class is still called! Console.ReadLine (); } }}
Output Result:
UsingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacenange_1{classA {Virtual Public voidF () {Console.WriteLine ("in the base class"); } } classb:a {Override Public voidF () {Console.WriteLine ("in the child class"); } } classProgram {Static voidMain (string[] args) {B OBJB=NewB (); A Obja=NewA (); OBJA.F (); the//base class calls its own method Obja= (A) objb;//add No (A) all the same! Base class reference points to derived class objectOBJA.F (); //Call the method of the derived class at this time! It's totally different from the top! Objb.f (); Console.ReadLine (); } }}
/*important information about the virtual and override modifiers is as follows: overwriting and overwriting methods must have the same accessibility! Static and non-virtual methods cannot be replicated. Methods, properties, indexers, and another type of member event (all can be declared as virtual and override)*/
Summary: this and C + + inside the virtual function effect is the same! It's called Polymorphism!
Virtual method and Overwrite method