【发布时间】:2021-02-25 02:21:39
【问题描述】:
假设我们有三个类:Person、Student extends Person 和 OfficeWorker extends Person。
public class Person{
//fields and methods, especially constructor & getter-setter methods
private String occupation;
}
public class Student extends Person{
private int education;
//Constructor assigns education zero to a student
public void dailyEducation(){
this.education++;
}
public void finishEducation(){
this.education = "Office Worker";
//_________________________________
}
}
public Class OfficeWorker extends Person{
//own fields and methods
}
public class mainF{
//typical psvm(s[]a)
}
那么我应该在 Student 类的空白处,甚至在 main 函数中填入什么内容,才能将人(学生)变成办公室工作人员?
【问题讨论】:
-
你可以让finishEducation返回一个Person,所以在这种情况下你会返回
new OfficeWorker()。但这不会改变现有对象的类型,你不能这样做。
标签: java class oop subclass superclass