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

2021-11-03  本文已影响0人  哈迪斯Java
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()+"正在上课...");
}

}

上一篇下一篇

猜你喜欢

热点阅读