用JAVA编写一个在校人员父亲(包变含量为职业,性别,年龄。全部为封装内部类),为别写子类:学生,教师,校工三个子类,学生有独立变量班级,教师有独立变量教研室,校工有独立变量工作所属部门。父类编写show方法展示信息,子类可重写方法或者直接用
时间: 2024-11-29 14:18:36 浏览: 9
在Java中,你可以创建一个名为`Personnel`的父类,作为员工的通用模板,然后为其提供一些基本属性如职业、性别和年龄。接着,我们可以为学生、教师和校工分别创建子类,并重写或扩展`show()`方法来显示特定的信息。下面是一个简单的示例:
```java
// 父类Personnel
public class Personnel {
private String profession;
private String gender;
private int age;
// 构造函数
public Personnel(String profession, String gender, int age) {
this.profession = profession;
this.gender = gender;
this.age = age;
}
// 展示基本信息的方法
public void show() {
System.out.println("职业:" + profession);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
}
}
// 子类Student
public class Student extends Personnel {
private String className;
public Student(String profession, String gender, int age, String className) {
super(profession, gender, age);
this.className = className;
}
@Override
public void show() {
super.show();
System.out.println("班级:" + className);
}
}
// 子类Teacher
public class Teacher extends Personnel {
private String researchRoom;
public Teacher(String profession, String gender, int age, String researchRoom) {
super(profession, gender, age);
this.researchRoom = researchRoom;
}
@Override
public void show() {
super.show();
System.out.println("教研室:" + researchRoom);
}
}
// 子类Staff
public class Staff extends Personnel {
private String department;
public Staff(String profession, String gender, int age, String department) {
super(profession, gender, age);
this.department = department;
}
@Override
public void show() {
super.show();
System.out.println("工作部门:" + department);
}
}
```
现在,每个子类都可以在其`show()`方法中添加特有的信息,并覆盖父类的行为。例如,学生会打印班级,教师会打印教研室,而校工会打印工作部门。
阅读全文