J2SE - super

Java中:

this 是对当前对象的引用

super是对当前对象的父类对象的引用

 

看这个例子:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. class FatherClass  
  2. {  
  3.         public int value;  
  4.         public void f()  
  5.         {  
  6.             value = 100 ;   
  7.             System.out.println("FatherClass.value" + value);      
  8.         }     
  9. }  
  10.   
  11. class ChildClass extends FatherClass  
  12. {  
  13.         public int value;  
  14.         public void f()  
  15.         {  
  16.             super.f();  
  17.             value = 200 ;  
  18.             System.out.println("ChildClass.value" + value);  
  19.             System.out.println(value);  
  20.             System.out.println(super.value);      
  21.         }     
  22. }  
  23.   
  24. Public class TestInherit  
  25. {  
  26.         public static void main(String[] args)  
  27.         {  
  28.             ChildClass cc = new ChildClass();  
  29.             cc.f();   
  30.         }     
  31. }  


内存分析:

 

运行完

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. ChildClass cc = new ChildClass();  

 

 

因为ChildClass继承了FatherClass,则ChildClass继承了FatherClass的所有成员变量和方法,所以ChildClass的内存中还有一个FatherClass。

 



当new出一个对象时,内存中就会有一个this引用,this引用指向对象自身,如果此对象继承父类,则还有一个super,super就指向父类对象

 

 

 

 

并不难懂的小知识,对不对! Over!

posted @ 2017-01-15 19:06  天涯海角路  阅读(119)  评论(0)    收藏  举报