[英]Casting generic return type to interface type
I am trying to create a factory method for a generic Wrapper and I have the problem that I either need to pass in the desired return type (wrapTyped()-method) or I have to explicitly cast an input argument to the desired return type (wrapAuto()-method, 1st use). 我正在尝试为通用包装器创建工厂方法,但遇到的问题是我要么需要传递所需的返回类型(wrapTyped()-方法),要么必须将输入参数显式转换为所需的返回类型( wrapAuto()方法,第一次使用)。 But I am lazy and don't want to write the extra cast :)
但是我很懒 ,不想写多余的演员:)
Is there any way to express the declaration of wrapAuto() so that case "wantThis" (at the very bottom) works? 有什么方法可以表达wrapAuto()的声明,以便案例“ wantThis”(在最底部)起作用?
public class GenericFactory {
static class Wrapper<T> {
T wrappdObject;
boolean flag;
String name;
public Wrapper(T wrappedObject, boolean flag, String name) {
this.wrappdObject = wrappedObject;
this.flag = flag;
this.name = name;
}
public T getObject() {
return wrappdObject;
}
// some more irrelevant methods
}
static interface InterfaceType {
}
static class ImplementationA implements InterfaceType {
}
static <U> Wrapper<U> wrapTyped(Class<U> type, U wrappedObject, boolean flag, String name) {
return new Wrapper<U>(wrappedObject, flag, name);
}
static <U> Wrapper<U> wrapAuto(U wrappedObject, boolean flag, String name) {
return new Wrapper<U>(wrappedObject, flag, "NoName");
}
// compiles, but is cumbersome
public Wrapper<InterfaceType> cumbersome = wrapTyped(InterfaceType.class, new ImplementationA(), true, "A");
// compiles, but is also cumbersome
public Wrapper<InterfaceType> alsoUgly = wrapAuto((InterfaceType) new ImplementationA(), true, "B");
// Want this, but "Type mismatch error"
public Wrapper<InterfaceType> wantThis = wrapAuto(new ImplementationA(), false, "C");
}
I stripped it down a little, for simplicity I declared only one set of interface and concrete implementation. 我将其简化了一些,为简单起见,我只声明了一组接口和具体实现。 I practice the class Wrapper may be used for many completely different, unrelated types.
我练习包装器类可用于许多完全不同的,不相关的类型。
In your method wrapAuto
, add another type parameter, with U
as upper bound, and use it as the formal parameter type: 在您的方法
wrapAuto
,添加另一个类型参数,以U
为上限,并将其用作形式参数类型:
static <U, T extends U> Wrapper<U> wrapAuto(T wrappedObject, boolean flag, String name) {
return new Wrapper<U>(wrappedObject, flag, "NoName");
}
and then this would work: 然后这将工作:
Wrapper<InterfaceType> wantThis = wrapAuto(new ImplementationA(), false, "C");
With this invocation, T
is inferred as ImplementationA
, and U
is inferred as InterfaceType
. 通过此调用,将
T
推断为ImplementationA
,将U
推断为InterfaceType
。 And the bounds T extends U
perfectly matches these types. T extends U
的边界与这些类型完全匹配。
References: 参考文献:
There is nothing wrong with your method as written. 您编写的方法没有任何问题。 But inference is not perfect.
但是推断并不完美。 You can always explicitly specify the type arguments:
您始终可以显式指定类型参数:
public Wrapper<InterfaceType> wantThis = GenericFactory.<InterfaceType>wrapAuto(new ImplementationA(), false, "C");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.