一、不同的指向
a.this指向当前对象的调用。
b.super指向当前调用对象的父类。
注意:super不是对象的引用,所以无法将super赋予给另一个对象变量。
二、不同的用法
a.this主要访问本类内容,用法有:访问本类的成员变量、成员函数、构造函数;
b.super则是在子类中访问父类,可以访问父类的成员变量,成员函数、构造函数;
注意:因为子类无法继承父类的构造方法,所以如果要调用父类的构造方法得必须在子类的构造方法体的第一行使用super( )方法。(super( )和this( )不能同时放在构造函数的第一行)
三、具体表现
a.this
class TestThis {
int a=0;
TestThis(int a) {
this.a = a;
}
TestThis( ) {
this(1);
}
public void giao( ) {
System.out. println("OHH,gaio");
}
public void wc( ) {
System.out.println("Wtf");
int b = this.a; //用this调用成员变量。
System.out.println("b的数值为:"+ b);
this.giao( );//用this调用成员变量。
}
public static void main(String[ ] args) {
new TestThis(2).wc();
}
}
b.super
public class Example {
Example( ) {
System.out.println("hh");
}
}
class Wal {
Wal( ) {
super( ); //默认寻找父类的构造器;
System.out.println("holle");
}
Wal(String yxh) {
super( );
System.out.println("WAN");
}
}
public class TestExample {
public static void main(String[ ] args) {
Wal a = new Wal( );
System.out.println("我来了,"+ a);
Wal b = new Wal("想念");
System.out.println(b);
}
}