封装,继承,多态综合应用

package com.Wzg;

public class Test13 {
    public static void main(String[] args) {
        System.out.println("================");
        person1[] p=new person1[4];//创建父类数组
        p[0]=new student("Morty",14,"男",1020);
        p[1]=new student("Summer",17,"女",1022);
        p[2]=new teacher1("Toddy",37,"男",13);
        p[3]=new teacher1("BoJack",44,"男",22);
        person1 temp;
        for (int i= 0; i <p.length-1; i++) {//冒泡排序
            for (int j= 0; j <p.length-1-i; j++) {
                if(p[j].getAge()>p[j+1].getAge()){
                    temp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=temp;
                }
            }
        }
        for (int i = 0; i <p.length; i++) {//输出排序后的数组
            System.out.println(p[i].info());
            System.out.println("================");
        }
        for (int i = 0; i <p.length; i++) {//调用指定对象中的私有方法
            if(p[i] instanceof student){
                student s=(student)p[i];//向下转型
                System.out.println(s.stu());
            }else {
                teacher1 t=(teacher1)p[i];//向下转型
                System.out.println(t.tea());
            }
        }
    }
}
class person1{
    private String name;
    private int age;
    private String sex;

    public person1(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    public String play(){
        return name+"爱玩";
    }
    public String info(){
        return "信息如下:\n"+"姓名:"+name+"\n年龄:"+age+"\n性别:"+sex;
    }
}
class student extends person1{
    private int stu_id;

    public student(String name, int age, String sex, int stu_id) {
        super(name, age, sex);
        this.stu_id = stu_id;
    }

    public String stu(){
        return "好好学习天天向上!";
    }
    public String play(){
        return super.getName()+"爱玩足球";
    }
    public String info(){
        return super.info()+"\n学号:"+stu_id+"\n"+stu()+"\n"+play();
    }
}
class teacher1 extends person1{
    private int work_age;

    public teacher1(String name, int age, String sex, int work_age) {
        super(name, age, sex);
        this.work_age = work_age;
    }
    public String tea(){
        return "子不教,父之过;教不严,师之惰!";
    }
    public String play(){
        return super.getName()+"爱下象棋";
    }
    public String info(){
        return super.info()+"\n工龄:"+work_age+"\n"+tea()+"\n"+play();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值