this关键字
this表示对当前对象的引用
一.在构造函数中,通过this可以调用同一类中别的构造函数。例如:
public class OverLoad
{
public OverLoad()
{
this(1);
System.out.println("test");
}
public OverLoad(int i)
{
i++;
System.out.println(i);
}
public static void main(String[] args)
{
OverLoad test = new OverLoad();
}
}
输出结果:
二.对本类中的成员变量进行操作。若要对OverLoad的成员变量x和y进行操作就应该用this进行引用。例如:
public class OverLoad
{
int x;
int y;
public OverLoad(int x,int y)
{
this.x=x;
this.y=y;
}
public static void main(String[] args)
{
OverLoad test = new OverLoad(1,2);
System.out.println(test.x+"\n"+test.y);
}
}
输出结果:
Super关键字
super表示对分类对象的引用。
当new一个对象出来的时候,这个对象会产生一个this的引用,这个this引用指向对象自身。如果new出来的对象是一个子类对象的话,那么这个子类对象里面还会有一个super引用,这个super指向当前对象里面的父对象。所以相当于程序里面有一个this,this指向对象自己,还有一个super,super指向当前对象里面的父对象。