3

我想用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 中的接口来实现这一点?

谢谢

4

5 回答 5

4

不,你不能这样做,你应该选择老师是 -> 员工是 -> 人。你不能在接口中实现任何东西!接口只能包含应该由类实现的方法。

于 2013-06-04T14:31:46.643 回答
3

你可以有这个:

public interface Person{
    // Only abstract methods here
}

public interface Employee extends Person {
    // Only abstract methods here specific to Employee
}

public class Teacher implements Employee {
    //Implements the methods 
}
于 2013-06-04T14:34:30.450 回答
2

Java 8 将允许您在接口中放置默认实现。在那之前,接口不能包含实现。

于 2013-06-04T14:29:54.807 回答
0
public interface Person {
    String getName();
    void setName(String name);
    int getAge();
    void setAge(int age);
}

public class PersonImpl implements Person {
    private String name;
    private int age;
    @override public String getName() { return this.name; }
    @override public void setName(String name) { this.name = name; }
    @override public int getAge() { return this.age; }
    @override public void setAge(int age) { this.age = age; }
}

public interface Employee extends Person {
    double getSalary();
    void setSalary(double salary);
    double calcSalary();
}

public abstract class EmployeeImpl extends PersonImpl implements Employee {
    private double salary;
    @override public double getSalary() { return this.salary; }
    @override public void setSalary(double salary) { this.salary = salary; }
}

public class Teacher extends EmployeeImpl
    @override public double calcSalary() { 
        if (title.equals("Professor")) salary=salary*0,30;
        else if (title.equals("Lecturer")) salary=salary*0,10;
    }
}
于 2013-09-16T01:10:58.603 回答
0

从人开始。你有一个错字:

public class Person
{
    private String name;
    private int age;
    public Person(String name, int age){
        this.name=name;
        this.age=age;
    }
    // set and get methods, equals, hashCode, toString,
    // perhaps an id for database storage.
}

现在,一些Persons 是Employees;其他人是学生、家长、准学生等等。如果任何方法Employee有一个实现,它就不能是一个接口。在任何情况下,拥有nameEmp都是错误的,因为它会在Person. 要么为 Employee 创建一个混入Teacher类的接口,要么从Employee.

任何一个:

public interface Employee {
    // Currency values should use BigDecimal or BigInteger, not double.
    BigDecimal salary();
    // Taxpayer Identification Number (SSN) in the USA, or the equivalent outside.
    String taxNumber();
}

或者:

public class Employee extends Person {

    private BigDecimal salary;
    private String taxNumber;
    public Employee(String name, int age, BigDecimal salary,
                    String taxNumber) {
        super(name, age);
        this.salary = salary;
        this.taxNumber = taxNumber;
        this.title = title;
    }
    // getters, setters, etc.
}

public class Teacher extends Employee {
    private Department department;
    private List<Course> courses = new ArrayList<>();
    // Constructors, getters, setters, etc.
}

如果需要完全不同的实现,您应该只在 Teacher 中使用薪水方法。您可能希望在 Employee 类中有某种薪酬计算器类(策略模式)。通过这种方式,您可以在适用的情况下处理应税扣除、保险、养老金计划和加班。我不理解您原始示例中的乘数。

于 2013-06-04T16:30:15.900 回答