|
public class OverrideTest {
public static void main(String[] args)
{
StudentCls studentCls = new StudentCls("zs",10,98);
System.out.println(studentCls.talk());
}
}
class StudentCls extends PersonCls {
float score;
public StudentCls(String name,int age,float score){
// this.score = score;//当将本句放在super(name,age)之前,IDEA工具会提示出错,只能放在后面
super(name,age); //初始化时在子类的构造函数中第一句就要super(...)对父类的构造函数进行调用,否则就会出错
this.score = score;
}
@Override
public String talk() {
// return "score = " + this.score;//这种做法就屏幕了父类中的talk方法调用
return super.talk() + ",score = " + this.score;//通过super.talk()表示对父类中的同名方法进行了调用
}
}
显示:
zs,今年:10岁,score = 98.0
|
|