public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
System.out.println("Person");
}
public String getName() {
return name;
}
abstract public String getDescription();
}
public class Student extends Person {
private String major;
public Student(String name, String major) {
super(name);
this.major = major;
}
public String getMajor() {
return major;
}
@Override
public String getDescription() {
return "student" + super.getName() + " having" + major;
}
}
public class PersonTest {
public static void main(String[] args) {
Person person = new Student("XYZ", "ABC");
System.out.println(person.getDescription());
}
}
問題:我們不能創建抽象類的對象,那麼爲什麼人類構造函數已被調用,甚至是抽象類?抽象類中的疑問
相關:http://stackoverflow.com/questions/2623180/abstract-class-and-constructor,http://stackoverflow.com/questions/2105783/what-is-the-use-of-creating-a -constructor-for-an-abstract-class-in-java,http://stackoverflow.com/questions/260666/abstract-class-constructor-in-java – finnw 2010-04-21 09:19:20
因爲它的抽象類可以有一些私有字段需要初始化。 – pmu 2010-04-21 11:08:35