1.this和super的区别
(1)代表不同
this代表的是当前对象的引用(所属函数的调用者对象)
super代表的是父类对象的引用
(2)使用前提不同
this不需要继承关系也能用
super必须有继承关系才能使用
(3)调用的构造函数不同
this调用所属类的构造函数
super调用的是父类的构造函数
2.this
public class A {
private String name;
private Integer age;
private String sex;
public A(String name,Integer age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
}
比如:
class A{
public void b(){
System.out.println(this);
}
public static void main(String[] args) {
A a = new A();
System.out.println(a);
a.b();
}
}
两次输出的值是一样的,a是对象A的引用,this也是对象A的引用。
3.super
class A{
int a = 1;
public void testA(){
System.out.println("aaaa");
}
}
class B extends A{
public void testB(){
System.out.println(super.a);
super.testA();
System.out.println("bbbb");
}
public static void main(String[] args) {
B b = new B();
b.testB();
}
}
1
aaaa
bbbb