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

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

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

Down

源码笔记

package com.tao.polymorphic_.poly_arr;


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;


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;


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;

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676682.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存