标签:
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 A 11 { 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 A 30 { 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 Test 47 { 48 public static void main(String[] args) 49 { 50 new B(); 51 } 52 } 53 54 /*output:s 55 56 create Mem: 9 57 ------------------------- 58 create A 59 org.tone.B@15db9742 org.tone.Mem@6d06d69c 9 60 create Mem: 99 61 org.tone.B@15db9742 org.tone.Mem@7852e922 99 62 create Mem: 88 63 ------------------------- 64 ************************************** 65 create B 66 org.tone.B@15db9742 org.tone.Mem@4e25154f 88 67 create Mem: 77 68 org.tone.B@15db9742 org.tone.Mem@70dea4e 77 69 15db9742 org.tone.Mem@70dea4e 77 70 ************************************** 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 A 13 { 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 A 32 { 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 Test 53 { 54 public static void main(String[] args) 55 { 56 new B(); 57 } 58 } 59 60 /*output:s 61 62 create Mem: 9 63 ------------------------- 64 create A 65 org.tone.B@15db9742 org.tone.Mem@6d06d69c 9 66 create Mem: 99 67 org.tone.B@15db9742 org.tone.Mem@7852e922 99 68 create Mem: 88 69 ------------------------- 70 ************************************** 71 create B 72 org.tone.B@15db9742 org.tone.Mem@4e25154f 88 73 create Mem: 77 74 org.tone.B@15db9742 org.tone.Mem@4e25154f 88 75 15db9742 org.tone.Mem@70dea4e 77 76 ************************************** 77 78 ************************************** 79 80 */
标签:
原文地址:http://www.cnblogs.com/itpoorman/p/4430659.html