Java基础之:OOP——多态练习题
应用案例1:
编写教师类
要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”
编写业务方法, introduce(),实现输出一个教师的信息。
编写教师类的三个子类:教授类(professor)、副教授类(vice professor)、讲师类(lecturer)。
工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。
定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。
package polymorphic_HomeWork;
public class WorkTest_01 {
public static void main(String[] args) {
Teacher professor = new Professor("小范",20,"professor",4000.0);
Teacher viceProfessor = new ViceProfessor("小黄",18,"ViceProfessor",3000.0);
Teacher lecturer = new Lecturer("小雨",19,"lecturer",2000.0);
System.out.println(professor.introduce());
System.out.println(viceProfessor.introduce());
System.out.println(lecturer.introduce());
}
}
//要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”,
//编写业务方法, introduce(),实现输出一个教师的信息。
class Teacher{
private String name;
private int age;
private String post;
private double salary;
public Teacher(String name, int age, String post, double salary) {
super();
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
}
public Teacher() {
super();
}
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 getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//introduce(),实现输出一个教师的信息。
public String introduce() {
return "Teacher [name=" + name + ", age=" + age + ", post=" + post ;
}
}
//教授类(professor)
class Professor extends Teacher{
private double level;
public Professor(String name, int age, String post, double salary) {
super(name, age, post, salary);
this.level = 1.3;
}
public double getLevel() {
return level;
}
public void setLevel(double level) {