Java中的多重继承问题

时间:2013-06-04 14:24:26

标签: java oop

我想用Java建模以下类:

enter image description here

所以我带来了以下代码:

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

由于

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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 
}

答案 2 :(得分:2)

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

答案 3 :(得分:0)

从Person开始。那里有一个错字:

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.
}

现在,有些PersonEmployee个;其他人是学生,家长,未来学生等。如果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.
}

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

答案 4 :(得分: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;
    }
}