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 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) { //当前运行类 Son Son son = new Son(); son.init(); //init(son) } }
1 init parent 4 demo son 3 init son <pre name="code" class="java">4 demo son
第二个例子:
public class Parent { public String name="tom"; public void init() { System.out.println(this.name); } }
public class Son extends Parent { public String name="jack"; public void init(){ super.init(); System.out.println(this.name); } public static void main(String[] args) { //当前运行类 Son Son son = new Son(); son.init(); //init(son) System.out.println("## " + son.name); Parent p = new Son(); System.out.println("** " + p.name); } }
tom jack ## jack ** tom
原文地址:http://blog.csdn.net/u010651186/article/details/38818383