Java课堂练习-面向对象-多态数组(基本运用)

Down

源码笔记

package com.tao.polymorphic_.poly_arr;

/**
 * Create By 刘鸿涛
 * 2021/12/17 22:33
 */
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 String say(){    //返回名字和年龄
        return name + "\t年龄:" + age + "\t";

    }
}

package com.tao.polymorphic_.poly_arr;

/**
 * Create By 刘鸿涛
 * 2021/12/17 22:35
 */
public class Student extends Person{
    private double score;

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
    //重写父类say
    @Override
    public String say(){
        return "学生->" + super.say() + "分数:" + score;
    }
    public void study(){
        System.out.println("学生" +super.getName() + "正在学习Java......");
        System.out.println("=========================");
    }
}

package com.tao.polymorphic_.poly_arr;

/**
 * Create By 刘鸿涛
 * 2021/12/17 22:37
 */
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 String say(){
        return "老师->" + super.say() + "薪水:" + salary ;
    }
    public void teach(){
        System.out.println("老师" + super.getName() + "正在授课....");
        System.out.println("=========================");

    }
}

package com.tao.polymorphic_.poly_arr;
/**
 * Create By 刘鸿涛
 * 2021/12/17 22:38
 */
public class PolyArray {
    //应用实例:现有一个继承结构如下:要求创建一个Person对象
    //2个Student对象和2个Teacher对象,统一放在数组中,并调用每个对象say()方法
    public static void main(String[] args) {
        Person[] persons = new Person[5];
        persons[0] = new Person("tom",20);
        persons[1] = new Student("jack",18,100);
        persons[2] = new Student("smith",19,30);
        persons[3] = new Teacher("scott",30,20000);
        persons[4] = new Teacher("king",50,25000);

        //循环遍历多态数组,调用say
        for (int i = 0; i < persons.length; i++){
            //persons[i] 编译类型是 Person,运行类型是根据实际情况由JVM来判断
            System.out.println(persons[i].say());   //动态绑定机制

            if(persons[i] instanceof Student){
                Student student = (Student)persons[i];  //向下转型
                student.study();
                //也可以使用一条语句 --(Student)persons[i].study();
            }else if(persons[i] instanceof Teacher){
                Teacher teacher = (Teacher)persons[i];
                teacher.teach();
            } else if(persons[i] instanceof Person){
                System.out.println("");
            }else{
                System.out.println("你的类型有误,请check out...");
            }
        }

    }

}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Top

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鬼鬼骑士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值