美文网首页
多态数组的应用||结合上下转型

多态数组的应用||结合上下转型

作者: 哈迪斯Java | 来源:发表于2021-11-03 09:44 被阅读0次
    image.png

    package HspLearningPoly.poly01.PolyArr;

    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;
    }
    

    }

    package HspLearningPoly.poly01.PolyArr;

    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;
    }
    public String say(){
        return super.say()+" salary= "+salary;
    }
    public void teach(){
        System.out.println("老师"+getName()+"正在上课...");
    }
    

    }

    相关文章

      网友评论

          本文标题:多态数组的应用||结合上下转型

          本文链接:https://www.haomeiwen.com/subject/zxjtzltx.html