簡體   English   中英

Java中的多重繼承問題

[英]issue with multiple inheritance in Java

我想用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中的接口實現這一點?

謝謝

不,你不能這樣做,你應該去老師是 - >員工是 - >人。 你無法在界面中實現任何東西! 接口只能包含應由類實現的方法。

你可以這樣:

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 
}

Java 8將允許您在接口中放置默認實現。 在此之前,接口不能包含實現。

從人開始。 那里有一個錯字:

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類(策略模式)。 通過這種方式,您可以在適用的情況下處理應稅扣除,保險,養老金計划和加班費。 我不明白原始例子中的乘數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM