求java代码

3.建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,包含学历、职业两项数据。官员则继承自成人,包含党派、职务两项数据,要求每个类的字段都以属性的方法对外提供数据输入输出功能。

1个回答

星尘闪烁的天空 2025-08-01 01:21:42
import java.util.Date;
class Person{
//身份证号
private String number;
//姓名
private String name;
//出生日期
private Date birthday;

public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
class Adult extends Person{
//学历
private String level;
//职业
private String work;
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
class Official extends Adult{
//党派
private String faction;
//职务
private String duty;
public String getDuty() {
return duty;
}
public void setDuty(String duty) {
this.duty = duty;
}
public String getFaction() {
return faction;
}
public void setFaction(String faction) {
this.faction = faction;
}
}
public class Test0404 {
public static void main(String[] args) {
Official o = new Official();
o.setNumber("1234567890");
o.setBirthday(new Date());
o.setName("张三");
o.setLevel("本科");
o.setWork("司机");
o.setFaction("XX党");
o.setDuty("接送领导");
//输出
System.out.println(o.getNumber());
System.out.println(o.getName());
System.out.println(o.getBirthday());
System.out.println(o.getLevel());
System.out.println(o.getWork());
System.out.println(o.getFaction());
System.out.println(o.getDuty());
}
}