1 class Mem 2 { 3 public int i; 4 public Mem(int i) 5 { 6 this.i = i; 7 System.out.println("create Mem: " + i); 8 } 9 }10 class A11 {12 public Mem mem = new Mem(9);13 14 public A()15 {16 System.out.println("-------------------------");17 System.out.println("create A");18 System.out.println(this + " " + this.mem + " " + this.mem.i);19 mem = new Mem(99);20 System.out.println(this + " " + this.mem + " " + this.mem.i);21 change();22 System.out.println("-------------------------");23 }24 public void change()25 {26 mem = new Mem(77);27 }28 }29 class B extends A30 {31 public B()32 {33 System.out.println("**************************************");34 System.out.println("create B");35 System.out.println(this + " " + this.mem + " " + this.mem.i);36 super.change();37 System.out.println(this + " " + this.mem + " " + this.mem.i);38 System.out.println(" " + Integer.toHexString(super.hashCode()) + " " + super.mem + " " + super.mem.i);39 System.out.println("**************************************");40 }41 public void change()42 {43 mem = new Mem(88);44 }45 }46 public class Test47 {48 public static void main(String[] args)49 {50 new B(); 51 }52 }53 54 /*output:s55 56 create Mem: 957 -------------------------58 create A59 org.tone.B@15db9742 org.tone.Mem@6d06d69c 960 create Mem: 9961 org.tone.B@15db9742 org.tone.Mem@7852e922 9962 create Mem: 8863 -------------------------64 **************************************65 create B66 org.tone.B@15db9742 org.tone.Mem@4e25154f 8867 create Mem: 7768 org.tone.B@15db9742 org.tone.Mem@70dea4e 7769 15db9742 org.tone.Mem@70dea4e 7770 **************************************71 72 */
1 package org.tone; 2 3 class Mem 4 { 5 public int i; 6 public Mem(int i) 7 { 8 this.i = i; 9 System.out.println("create Mem: " + i);10 }11 }12 class A13 {14 public Mem mem = new Mem(9);15 16 public A()17 {18 System.out.println("-------------------------");19 System.out.println("create A");20 System.out.println(this + " " + this.mem + " " + this.mem.i);21 mem = new Mem(99);22 System.out.println(this + " " + this.mem + " " + this.mem.i);23 change();24 System.out.println("-------------------------");25 }26 public void change()27 {28 mem = new Mem(77);29 }30 }31 class B extends A32 {33 // 此处定义mem没有初始化,可以在父类构造函数中初始化该变量, 如果在此处或者B类构造方法中初始化了, 则会覆盖父类构造中的初始化;34 // 此处定义的mem与父类中定义的mem都同时存在于B类中, 其实B类中的super和this指向的是同一个对象(他们的hashcode值是一样的), 35 // 只是如果类属性或方法重名的话,通过他们访问属性时,他们会分别找到不同类中定义的属性或方法36 public Mem mem; 37 public B()38 {39 System.out.println("**************************************");40 System.out.println("create B");41 System.out.println(this + " " + this.mem + " " + this.mem.i);42 super.change();43 System.out.println(this + " " + this.mem + " " + this.mem.i);44 System.out.println(" " + Integer.toHexString(super.hashCode()) + " " + super.mem + " " + super.mem.i);45 System.out.println("**************************************");46 }47 public void change()48 {49 mem = new Mem(88);50 }51 }52 public class Test53 {54 public static void main(String[] args)55 {56 new B(); 57 }58 }59 60 /*output:s61 62 create Mem: 963 -------------------------64 create A65 org.tone.B@15db9742 org.tone.Mem@6d06d69c 966 create Mem: 9967 org.tone.B@15db9742 org.tone.Mem@7852e922 9968 create Mem: 8869 -------------------------70 **************************************71 create B72 org.tone.B@15db9742 org.tone.Mem@4e25154f 8873 create Mem: 7774 org.tone.B@15db9742 org.tone.Mem@4e25154f 8875 15db9742 org.tone.Mem@70dea4e 7776 **************************************77 78 **************************************79 80 */
创建子类对象时,会调用父类的构造方法, 构造方法中调用的某个方法如果子类中覆盖了, 则会调用子类中对应的方法,否则则调用父类中的方法(注:private方法是无法被override的)