我想用Java建模以下类:
所以我提供了以下代码:
class Person
{
private String name;
private ing age;
public Person(String name, int age){
this.name=name;
this.age=age;
}
//set and get methods
}
class Employee
{
private String nameEmp;
private double salary;
public Employee(String nameEmp, double salary){
this.nameEmp=nameEmp;
this.salary=salary;
}
public double calcSalary(){} //should this be an abstract method?
}
class Teacher extends Person implements Employee
{
private String nameTeacher;
private int ageTeacher;
private String title; //professor or lecturer
public Teacher(String nameTeacher,int ageTeacher, String title){
super(nameTeacher,ageTeacher);
this.title=title;
}
public double calcSalary(){
if (title.equals("Professor")) salary=salary*0,30;
else if (title.equals("Lecturer")) salary=salary*0,10;
}
}
我想使用接口对其进行建模,但我不太确定该怎么做。calcSalary 也应该是 Employee 中的抽象方法?如何使用 Java 中的接口来实现这一点?
谢谢