编写老师类Homework03.java
(1)要求有属性“姓名name”,“年龄age”,“职称post",“基本工资salary"(⑵)编弓业务方法,introduce (),实现输出一个教师的信息。
(3)编写教师类的三个子类:教授类(Professor )、副教授类、讲师类。工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce ()方法。(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印.
public class Main {
public static void main(String[] args) {
Teacher[] teachers = new Teacher[3];
teachers[0] = new Lecturer("张三",33,"讲师",1231);
teachers[1] = new AssociatePprofessor("李四",44,"副教授",1345);
teachers[2] = new Professor("王五",55,"教授",23311);
for (int i = 0; i < teachers.length; i++) {
if (teachers[i] instanceof Professor){
teachers[i].introduce();
System.out.println();
}
else if (teachers[i] instanceof AssociatePprofessor){
teachers[i].introduce();
System.out.println();
}
else if (teachers[i] instanceof Lecturer){
teachers[i].introduce();
System.out.println();
}else {
System.out.println("无法确认教师级别!");
}
}
}
}
class Teacher {
private String name;
private int age;
private String post;//职称
private double salary;
public Teacher(String name, int age, String post, double salary) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
}
public void introduce() {
System.out.print("name: " + this.name + " age: " + this.age + " post:" + this.post + " salary: " + this.salary );
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", post='" + post + '\'' +
", salary=" + salary +
'}';
}
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;
}
}
//教授类
class Professor extends Teacher {
private String payScale = "1.3";//工资等级
public Professor(String name, int age, String post, double salary) {
super(name, age, post, salary);
}
@Override
public void introduce() {
super.introduce();
System.out.println( " payScale:" + this.payScale);
}
}
class AssociatePprofessor extends Teacher{
private String payScale = "1.3";//工资等级
public AssociatePprofessor(String name, int age, String post, double salary) {
super(name, age, post, salary);
}
@Override
public void introduce() {
super.introduce();
System.out.print(" payScal:" +this.payScale);
}
}
class Lecturer extends Teacher{
private String payScale = "1.3";//工资等级
public Lecturer(String name, int age, String post, double salary) {
super(name, age, post, salary);
}
@Override
public void introduce() {
super.introduce();
System.out.print(" payScal:" +this.payScale);
}
}
编译结果