简体   繁体   English

如何将这种多重继承转换为Java接口?

[英]how to convert this multiple inheritance to interfaces in Java?

I want to implement the following multiple inheritance case: 我想实现以下多重继承的情况:

(took from http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/ ) (摘自http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/

Person           Employee
   |_________________|
              |
           Nurse

at the end I want to print the salaries from Employee and Nurse. 最后,我想打印雇员和护士的薪水。 In C++ it is easy to be done by using multiple inheritance, but I have problems with Java. 在C ++中,使用多重继承很容易完成,但是Java存在问题。

I have the following codes: 我有以下代码:

public class Person{
    protected String name;
    protected int age;
    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }
}

and the interface: 和界面:

public interface Employee{
    public double getSalary();
}

and the class Nurse: 和班上的护士:

public class Nurse extends Person implements Employee{
    private double salary;
    public Nurse(String name, int age, double salary){
        super(name,age);
        this.salary=salary;
    }
    @Override
    public double getSalary(){
        return salary;
    }
}

but I do not have a clue how to make the Employee to print its salary, because it is an interface. 但是我不知道如何使雇员打印工资,因为它是一个界面。 I do not want to use other abstract class called Employee. 我不想使用其他称为Employee的抽象类。 How to fix this? 如何解决这个问题?

Thanks 谢谢

Assuming that all employees are people (unless you are hiring monkeys!!), could you not chain the classes? 假设所有员工都是人(除非您正在雇用猴子!),您是否不能将课程连锁?

public class Person {
    private String name;
    private int age;

    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
}

public class Employee extends Person {
    private double salary;

    public double getSalary(){
        return salary;
    }
}

public class Nurse extends Employee {
}

Java allows multiple inheritance of interfaces only, so in order to implement that you have to define an interface for every class you'd like to combine and compose them in a single class by delegating all methods derived from the partial interfaces to their implementation. Java只允许接口的多重继承,因此,要实现该接口,必须为每个要定义的接口定义一个接口,方法是将部分接口派生的所有方法委派给它们的实现,以将它们组合并组成一个类。

Person interface - as you've defined it 人机界面 -如您所定义

PersonImpl class : PersonImpl类

public final class PersonImpl implements Person {
    private final String name;
    private final int age;

    public PersonImpl(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getAge() {
        return age;
    }
}

EmployeeImpl class : EmployeeImpl类

public final class EmployeeImpl implements Employee {
    private final double salary;

    public EmployeeImpl(double salary) {
        this.salary = salary;
    }

    @Override
    public double getSalary() {
        return salary;
    }
}

Nurse class - composed from to other classes and delegates the functionality to them: 护士班 -由其他班级组成,并向他们委托功能:

public class Nurse implements Employee, Person {

    private final Employee employee;
    private final Person person;

    public Nurse(String name, int age, double salary) {
        person = new PersonImpl(name, age);
        employee = new EmployeeImpl(salary);
    }

    @Override
    public double getSalary() {
        return employee.getSalary();
    }

    @Override
    public String getName() {
        return person.getName();
    }

    @Override
    public int getAge() {
        return person.getAge();
    }
}

It's advisable to define an interface for every class you code in order to able that approach in the future. 建议为您编写的每个类定义一个接口,以便将来能够使用该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM