栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/671447.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号