gpt4 book ai didi

java - 子接口(interface)如何重用其父接口(interface)的实现?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:43 26 4
gpt4 key购买 nike

最近我参加了一个面试,被问到以下问题。给定以下类/接口(interface)结构:

enter image description here

问题:

如何实现接口(interface) EmployedStudent 以重用来自 StudentImplEmployeeImpl 的代码。

我建议将 Employee 和 Student 组合到我的实现中。

根据面试官的 react ,我认为他们认为这不是最佳解决方案。我花了很多时间思考它,但我无法想出其他解决方案。

最佳答案

创建一个同时实现EmployeeStudent 的类。在您的类中,创建 EmployeeImplStudentImpl 的实例。然后让您的类将所有方法调用委托(delegate)给其中一个对象。

public class EmployedStudent implements Employee, Student {

private EmployeeImpl employee = new EmployeeImpl();
private StudentImpl student = new StudentImpl();

public int getSalary() {
return this.employee.getSalary();
}

public float getAverageGrade() {
return this.student.getAverageGrade();
}

}

关于java - 子接口(interface)如何重用其父接口(interface)的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28967796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com