[英]Inconvertible types; cannot cast 'caputure<? extends IEntity>'to 'java.lang.Class<T>'
There are three class,class IEntity is an abstract class,which is only used for generic.共有三个 class,class IEntity 是一个抽象的 class,仅用于泛型。
Class one: Class 一:
public abstract class IEntity {
}
class two: class 二:
public class Config {
private Class<? extends IEntity> clazz;
public Class<? extends IEntity> getClazz(){
return this.clazz;
}
}
class three: class 三:
public class EntityTest<T extends IEntity> {
private Class<T> clazz;
public void init(Config config){
//Here is the wrong report
// Failed to compile
this.clazz=config.getClazz();
}
}
It seems like you have not understood the difference between <? extends IEntity>
看来你还没有理解
<? extends IEntity>
<? extends IEntity>
and <T extends IEntity>
. <? extends IEntity>
和<T extends IEntity>
。
Let's introduce two subclasses of IEntity
, and a constructor for Config
for a clearer explanation:让我们介绍
IEntity
的两个子类,以及Config
的构造函数以便更清楚地解释:
class Entity1 extends IEntity {}
class Entity2 extends IEntity {}
// in Config class
public Config(Class<? extends IEntity> clazz) {
this.clazz = clazz;
}
At the line this.clazz=config.getClazz();
在这一行
this.clazz=config.getClazz();
, you are trying to assign a Class<? extends IEntity>
,您正在尝试分配一个
Class<? extends IEntity>
Class<? extends IEntity>
to a Class<T>
, where T
is a subclass of IEntity
or IEntity
itself. Class<? extends IEntity>
为Class<T>
,其中T
是IEntity
或IEntity
本身的子类。 The problem is, we don't know exactly what type of class getClazz
returns.问题是,我们不知道究竟是什么类型的 class
getClazz
返回。 It could be Class<Entity1>
, or Class<Entity2>
.它可以是
Class<Entity1>
或Class<Entity2>
。 On the other hand, we do know what type we need - Class<T>
.另一方面,我们确实知道我们需要什么类型 -
Class<T>
。 How can we make sure that whatever getClazz
returns, is the same type of class as Class<T>
?我们如何确保无论
getClazz
返回什么类型的 class 与Class<T>
相同? T
could be Entity2
but getClazz
could return Class<Entity2>
, couldn't it? T
可以是Entity2
但getClazz
可以返回Class<Entity2>
,不是吗?
Here's a concrete example with code:这是一个带有代码的具体示例:
Config c = new Config(Entity1.class);
// c.config now contains Entity1.class
// the line below will attempt to assign an Entity1.class to a variable of type Class<Entity2>
new EntityTest<Entity2>().init(c);
You should now see why there is an error in init
.您现在应该明白为什么
init
中有错误。
One way to make sure that getClazz
returns the same type of class as Class<T>
is to make Config
generic too:确保
getClazz
返回与Class<T>
相同类型的 class 的一种方法是使Config
也通用:
class Config<T extends IEntity> {
private Class<T> clazz;
public Class<T> getClazz(){
return this.clazz;
}
}
class EntityTest<T extends IEntity> {
private Class<T> clazz;
public void init(Config<T> config){
this.clazz=config.getClazz();
}
}
<? extend IEntity>
<? extend IEntity>
indicates that the class must be IEntity or it's subclass. <? extend IEntity>
表示 class 必须是 IEntity 或其子类。 The "T" in your code is just one subclass of IEntity(there can be many subclasses of <? extend IEntity>
), you could not be sure that <? extend IEntity>
您代码中的“T”只是 IEntity 的一个子类(
<? extend IEntity>
可以有很多子类),您不能确定<? extend IEntity>
<? extend IEntity>
is same as "T" or is subclass of "T". <? extend IEntity>
与“T”相同或者是“T”的子类。 so the type cast is illegale.所以类型转换是非法的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.