在我正在使用的一些代码中,我有一个现有的第三方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...
}发布于 2014-02-11 00:06:27
使用泛型助手类,其类型参数允许您使用类型交集:
class Z {
private static final class Wrapper<T extends A & IB> {
private final T item;
Wrapper(final T item) {
this.item = item;
}
void doWork() {
// code thats requires facilities from A and IB
}
}
private List<Wrapper<?>> wrappers;
private <T extends A & IB> void subscribe(T item) {
wrappers.add(new Wrapper<T>(item));
}
public void doSomethingWithItems() {
for (final Wrapper<?> wrapper : wrappers) {
wrapper.doWork();
}
}我在这篇文章中给出了类似的答案:Java generics type mismatch in method signature
发布于 2014-02-10 23:58:09
可以指定类型交集。
<T extends A & IB>规则是,如果其中一个类型是类,则必须首先列出它。
如果你能的话,我会键入这门课:
class Z<T extends A & IB> {
private List<T> items;
private void subscribe(T item) {
items.add(item);
}
public void doSomethingWithItems() {
// the items are both A and IB
}
}如果不能键入Z,请选择类型化方法:
class Z {
private List<A>items;
private <T extends A & IB> void subscribe(T item) {
items.add(item);
}
public void doSomethingWithItems() {
// items are A, but if you want IB functionality you must cast.
// the cast is safe if items are only added via subscribe()
}
}发布于 2014-02-11 00:20:11
如果IB是IA的一个亚型,那将是最理想化的,但如果你做不到.
这很难用代码来表达,因为Java不支持第一类交叉类型。也就是说,Java只支持类型参数边界中的交集类型,而且我们不能在字段声明中使用类型参数,而不要求列表中的所有元素都是IB的同一子类型。
因此,我能想到的最佳近似方法是仅在公共API中强制执行约束,并在内部使用强制转换:
class Z {
private List<A> items;
private <B extends A & IB> void subscribe(B item) {
items.add(item);
}
public void doSomethingWithItems() {
for (A a : items) {
IB b = (IB) a; // safe because we checked it on subscription
// use features of A and IB
}
}
}是的,很丑。这就是为什么较新的JVM语言(如锡兰特性)具有第一类交叉类型的原因。也就是说,在锡兰,人们可以简单地写:
class Z() {
List<A & IB> items;
void subscribe(A & IB item) {
items.add(item);
}
void doSomethingWithItems() {
for (item in items) {
// use features of A and IB
}
}
}https://stackoverflow.com/questions/21690399
复制相似问题