【发布时间】:2014-02-10 23:55:13
【问题描述】:
在我正在使用的一些代码中,我有一个现有的第三方 API,它实现了从 A 扩展的东西(也许不是直接的,而是通过 X,也许还实现了一堆其他接口)。
现在对于我正在处理的代码,我有一个接口 IB,它为 A 提供的功能提供了额外的功能。因此,我的很多代码实际上都要求传递给它的对象扩展 A,并且还实现了 IB,但是对于我能想到的成员变量,没有办法声明它。但是选择 A 或 IB 会导致很多演员。
我猜如果 A 是/有一个接口 IA 会解决这个问题,但是我无法更改 A,或者我的 IB 实现不需要扩展 A(第三方代码使用 A,并注意通过它进行大量管理、持久性、网络、用户界面等)。
class Z {
private List<?what here?> items;
/**The implementer of IB should know how to find the Z instance and call this.*/
private void subscribe(? item) {
items.add(item);
}
public void doSomethingWithItems() {
...code thats requires facilities from A and IB...
}
【问题讨论】:
标签: java generics inheritance interface