super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
this关键字:指向自己的引用。
//父类
public class FengZhuang {
public static void main(String[] args) {
FengZhuang fengZhuang = new FengZhuang();
}
public int intA = 5;
public String strB = "五";
public void printStr(){
System.out.println("打印");
}
}
//子类继承父类
public class Test extends FengZhuang{
public int intA = 9;
public static void main(String[] args) {
//实例化
Test test = new Test();
test.print();//调用方法
}
public void print(){
//super 子类中调用父类的方法或成员变量
System.out.println("super.intA: " + super.intA);
System.out.println("super.strB: " + super.strB);
super.printStr();
//this 调用自身的方法或成员变量
this.print(super.strB);
System.out.println("this.intA: " + this.intA);
}
public void print(String str){
System.out.println("str: " + str);
}
}
执行完后输出结果如下: