this & super

/*
当本类的成员和局部变量同名用this区分。
当子父类中的成员变量同名用super区分父类。
 
this和super的用法很相似。
 
this:代表一个本类对象的引用
super:代表一个父类空间
 
*/
class Fu
{
private int num = 4;
 
public int getNum()
{
return num;
}
}
 
 
class Zi extends Fu
{
private int num = 5;
 
 
void show()
{
System.out.println(this.num+"....."+super.getNum());  //可以看出:父类中的private成员变量
                                                                                                    //依然存在于子类的对象中。
}
}
 
 
class ExtendsDemo2 
{
public static void main(String[] args) 
{
Zi z = new Zi();
z.show();
}
}

 

 
this  super - darrell - DARRELL的博客
posted @ 2018-01-17 22:42  darrell007  阅读(118)  评论(0)    收藏  举报