public class Parent {public void init () {System.out.println ("1 init Parent"); This.demo ();} public void Demo () {System.out.println ("2 demo parent");}}
public class Son extends the Parent {public void init () {super.init (); System.out.println ("3 init son"); This.demo ();} public void Demo () {System.out.println ("4 demo Son");} public static void Main (string[] args) {//Current run class Sonson son = new son (); Son.init (); Init (son)}}
The output is:
1 init parent4 demo son3 init son<pre name= "code" class= "Java" >4 demo son
When manipulating a method, this refers to the current running class, which in this case refers to son;
A second example:
public class Parent {public String name= "Tom";p ublic void init () {System.out.println (this.name);}}
public class Son extends the Parent {public String name= "Jack";p ublic void init () {super.init (); System.out.println (this.name);} public static void Main (string[] args) {//Current run class Sonson son = new son (); Son.init (); Init (son) System.out.println ("# #" + son.name); Parent p = new Son (); SYSTEM.OUT.PRINTLN ("* *" + p.name);}}
The output is:
tomjack## jack** Tom
When manipulating a field, it cannot be overridden as a method, whose is Who.
The This action method and field in Java