java多态数组

父类:



public class Person {   //父类
   private  String name;
    private int age;

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

    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 void say()
    {
        System.out.println(name+"\t"+age);
    }
}

子类:



public class Student extends Person {
     private double score;

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Student(String name, int age, double score) {
        super(name, age);
        this.score = score;
    }

    @Override //重写注解
    public void say()
    {
        System.out.println(getName()+"\t"+getAge()+"\t"+score);
    }
    public void study()
    {
        System.out.println("学生"+getName()+"正在学习java");
    }
}

子类:



public class Teacher extends Person{
    private double salary;

    public Teacher(String name, int age, double salary) {
        super(name, age);
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public void say()
    {
        System.out.println(getName()+"\t"+getAge()+"\t"+salary);
    }
    public void teach()
    {
        System.out.println("老师"+getName()+"正在讲课");
    }


}

测试类:



public class Test {
    public static void main(String[] args) {
        //多态数组
        Person[] person = new Person[5]; //创建对象并实例化
        person[0] =  new Person("李白",18);
        person[1] =  new Student("韩信",99,88.8); //向上转型
        person[2] =  new Student("露娜",18,99.9);//向上转型
        person[3] =  new Teacher("老子",66,2000);//向上转型
        person[4] =  new Teacher("墨子",88,5000);//向上转型

        //循环遍历多态数组
        for(int i=0;i<person.length;i++)
        {
         person[i].say(); //动态绑定机制
            //运用java关键词 instanceof 关键词
        if(person[i] instanceof Student)
            {
                //就向下转型(上面已经向上转型)
                Student student = (Student)person[i]; //向下转型
                student.study();
            }
        else if(person[i] instanceof Teacher)
            {
                //重复上面的过程
                Teacher teacher = (Teacher)person[i];
                teacher.teach();
            }
        else
            System.out.println("输入错误!");
        }

    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小雨527

你的鼓励是我的最大的动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值