Question: In a single parent class method, a virtual method call to the parent class (this method is overridden by subclasses of), the execution of this virtual method is the parent class methods out of it or a subclass of it?
class Father { public String name = " my father " ; public Virtual String GetName () { return the this .name; } public void SetFather () { var the _name = the this .getName (); // if the object is a subclass instance if , a method where tone is to rewrite subclass; if the object is the parent class instance, then, of course tune parent class Console.WriteLine ( " Get value by GetName method: " + the _name); Console.WriteLine ( " Father's name is: " +name); } } class Son: Father { public String son_name = " I am a son " ; public the override String GetName () { the this .son_name = " son become a pig " ; the this .name = " father's name changed oh " ; return the this .son_name; } }
transfer:
/ * * This is mainly a single test method in the parent class, a virtual method call parent class (which is overridden by subclasses), then the virtual method is performed off * parent class or subclass it? * Experiments show that: the tune is for subclasses to be rewritten. * / Console.WriteLine ( " instantiated objects son " ); Father F = new new Son (); f.SetFather (); Console.WriteLine ( " ================= ======= I am gorgeous dividing line ======================== " ); Console.WriteLine ( " instantiate an object father . " ); Father F2 = new new Father (); f2.SetFather (); Console.Read();
The answer is: If the object is a subclass of example, here is emphasized that subclass override method; if the object is the parent class instance, then, of course tune parent class