当前位置:首页 > IT技术 > 编程语言 > 正文

JavaSE基础之继承
2022-03-06 18:12:26

比如在子类中定义这样一个方法

public void test1(){
print();//student
this.print();//student
super.print();//person
}
public void print() {
System.out.println("STUDENT");
}
区别:
print();是调用这个类中的方法
this.print();是调用这个类中的方法
super.print();是调用父类中的print方法

public Student() {
//隐藏代码 默认调用了父类的无参构造
super();//调用父类的构造器,必须要在子类构造器的第一行
System.out.println("STUDENT无参构造执行了");
}

注意:
在调用父类的构造器时,super();必须在子类构造器的第一行




 




 

本文摘自 :https://www.cnblogs.com/