下图程序中
空(1) 答案是填 abstract double getGPA()
答案本身是没错的,问题是这里有没有必要加这一句,定义这么一个抽象的getGPA方法
我自己写了个类似的代码试了一下(在下面),发现去掉这一句照样可以正常运行。
这里定义一个抽象方法,我怀疑是不是这样一来,子类就被迫必须实现这个抽象方法了



package pe201301;
abstract class Student{
protected String name;
protected int stuNo;
protected double GPA;
protected int grades;
public Student(int stuNo, String name, int grades){
this.stuNo = stuNo;
this.name = name;
this.grades = grades;
}
//abstract double getGPA();
double computeWg(){
return grades*100;
}
}
class ActStudent extends Student{
private int Apoints;
ActStudent(int stuNo, String name, int grades, int Apoints){
super(stuNo,name,grades);
this.Apoints = Apoints;
}
public double getGPA(){
return GPA = grades*100;
}
}
public class Java {
public static void main(String[] args) {
ActStudent a = new ActStudent(101,"zhang",89,10);
System.out.println(a.getGPA());
}
}
------解决思路----------------------
抽象类是指不能创建对象,不能能实例化的类,用abstract修饰就可以了,和方法没有直接关系,抽象类里可以没有抽象方法,不过包含抽象方法的类一定是抽象类。
------解决思路----------------------
抽象类可以没有抽象方法,甚至可以和普通类一样含有构造方法。当你要求不能创建某个类的实例,只能通过其他类继承这个类来使用时,就可以定义为抽象类。
------解决思路----------------------
如果子类不覆盖父类的抽象方法,子类也必须是抽象类
------解决思路----------------------
没错!但子类就不用引用GPA?了
------解决思路----------------------
抽象方法所在的类一点为抽象类,但抽象类中的方法不一定都是抽象方法,也就是不一定非要有抽象方法。其实楼主遇到这样的问题可以自己去编辑工具中测试一下印象会更加深刻。