我正在尝试填写附录Appendix。我只是想知道我是否按照附录正确编码并且我使用了正确的方法。我不确定我是否在兴趣()下做了正确的事情。在哪里我称超级课是正确的?
public interface LoanInterest {
double interest();
String getName();
String toString();
} //end of LoanInterest
public abstract class Student implements LoanInterest {
private String name;
private String studentNumber;
private double feesOwed;
public Student(String nm, String num, double amt) {
name = nm;
studentNumber = num;
feesOwed = amt;
}
public double getFeesOwed() {
return feesOwed;
}
public String getStudentNumber() {
return studentNumber;
}
public String getName() {
return name;
}
public String toString() {
String msg;
msg = String.format("%s\t%s\t%.2f", name, getStudentNumber(), getFeesOwed());
return msg;
}
} //end of Student
public class UnderGrad extends Student {
public UnderGrad(String nm, String num, double amt) {
super(nm, num, amt);
}
public double interest() {
return super.getFeesOwed() + (super.getFeesOwed() * 0.14);
}
} //end of UnderGrad
public class PostGrad extends Student {
private String diploma;
public PostGrad(String nm, String num, double amt) {
super(nm, num, amt);
}
public String getDiploma() {
return diploma;
}
public double interest() {
return super.getFeesOwed() + (super.getFeesOwed() * 0.14);
}
} //end of PostGrad
答案 0 :(得分:1)
您不需要致电super.methodName
,因为您没有在PostGrad
或UnderGrad
中覆盖它们,但它不是"错误"任
所以你可以写:
public double interest() {
return getFeesOwed() + (getFeesOwed() * 0.14);
}
如果你要覆盖它们,你很可能也想要使用它们,所以再次没有super
。
如果方法被覆盖为添加一些附加功能,则通常会使用super
关键字,而不会完全重写被覆盖方法的代码。