多态机制练习

package com.hspedu.encap;

//测试类
public class test {
    public static void main(String[] args) {
        Person[] persons = new Person[5];  //Person 类
        //编译类型         运行类型
        persons[0] = new Person("laLa",17); 
        persons[1] = new Student("koKo",17,77);
        persons[2] = new Student("qiQi",19,97);
        persons[3] = new Teacher("qinQin",39,1000);
        persons[4] = new Teacher("kaKa",29,1000);

        for (int i = 0; i < persons.length; i++) {
            System.out.println(persons[i].say());  //遍历数组 将每个方法输出
            //如果 perosns[i] 的运行类型 是 Student 
            if (persons[i] instanceof Student) { 
                Student student = (Student)persons[i];
                student.study();  //则调用这个方法
                //如果 perosns[i] 的运行类型 是 Teacher 
            } else if (persons[i] instanceof Teacher) {
                Teacher teacher = (Teacher)persons[i];
                teacher.teach();  //则调用这个方法
                //如果 perosns[i] 则什么都不发生
            } else if (persons[i] instanceof Person){ 

            } else {
                System.out.println("写错了,宝儿");
            }
        }
    }
}
package com.hspedu.encap;

//父类 公共类
public class Person {
    //共有属性
    private String name;
    private int age;

    //构造器
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    //get set方法
    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 + " 年龄:" + age;
    }
}
package com.hspedu.encap;

//学生类
public class Student extends Person{
    //子类独有的属性
  private double score;

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

    //get set 方法
    public double getScore() {
        return score;
    }

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

    //学生类独有的方法
    @Override
    public String say() {
        return " 学生:" + super.say() + " 成绩:" + score;
    }

    //学生类单独调用的方法
    public void study() {
        System.out.println("学生" + getName() + "在学习");
    }
}
package com.hspedu.encap;

//老师类
public class Teacher extends Person{
    //老师类独有的属性
    private double salary;

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

    //get set 方法
    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(" 老师: " + getName() + " 在教课 ");
    }
}

 

posted @ 2023-01-05 22:05  书先生  阅读(22)  评论(0)    收藏  举报