「Java学习打卡」30、3.25面向对象程序练习

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

class Person {
    public String no;
    public String name;
    public String birth;

    public Person(String no, String name, String birth) {
        PNO(no);
        PN(name);
        PB(birth);
    }

    public void PNO(String no) {
        System.out.println("身份证号:" + no);
        this.no = no;
    }

    public void PN(String name) {
        System.out.println("姓名:" + name);
        this.name = name;
    }

    public void PB(String birth) {
        System.out.println("出生日期:" + birth);
        this.birth = birth;
    }

}

class Adult extends Person {
    public String study;
    public String job;

    public Adult(String no, String name, String birth, String study, String job) {
        super(no, name, birth);
        AST(study);
        AJO(job);
    }

    public void AST(String study) {
        System.out.println("学历:" + study);
        this.study = study;
    }

    public void AJO(String job) {
        System.out.println("职业:" + job);
        this.job = job;
    }
}

class Official extends Adult {
    public String political;
    public String post;

    public Official(String no, String name, String birth, String study, String job, String political, String post) {
        super(no, name, birth, study, job);
        OPol(political);
        OPos(post);
    }

    public void OPol(String Political) {
        System.out.println("党派:" + Political);
        this.political = Political;
    }

    public void OPos(String post) {
        System.out.println("职务:" + post);
        this.post = post;
    }
}

public class Test {
    public static void main(String args[]) {
        Person p =new Person("4************1", "张三", "1998-08-08");
        System.out.println();
        Adult a = new Adult("4************2", "李四", "1999-08-08", "本科", "监工");
        System.out.println();
        Official o = new Official("4************3", "王五", "1996-08-08", "本科", "公务员", "共产党", "办事人员");

    }
}
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值