简体   繁体   English

不可转换的类型; 不能施放'捕获<!--? extends IEntity--> '到'java.lang.Class<t> '</t>

[英]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> ,其中TIEntityIEntity本身的子类。 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可以是Entity2getClazz可以返回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.

相关问题 无与伦比的类型:java.lang.Class<capture#1 of ? extends T[]> 和 java.lang.Class<java.lang.Object[]> - Incomparable types: java.lang.Class<capture#1 of ? extends T[]> and java.lang.Class<java.lang.Object[]> EasyMock Java:不兼容的类型:java.lang.ClassLoader无法转换为java.lang.Class <capture#1 of ? extends > - EasyMock java: incompatible types: java.lang.ClassLoader cannot be converted to java.lang.Class<capture#1 of ? extends > “无法转换为java.lang.Class”,即:哪种类型不是类 - “Cannot be cast to java.lang.Class”, namely: which type is not a class Java:ClassCastException-无法将java.lang.Class强制转换为 - Java: ClassCastException - java.lang.Class cannot be cast to 未选中的强制转换:&#39;java.lang.Class <capture<?> &gt;&#39;to&#39;java.lang.Class <T> - Unchecked cast: 'java.lang.Class<capture<?>>' to 'java.lang.Class<T> 不可转换的类型 不能强制转换 - Inconvertible types Cannot cast 无法从java.lang.Class转换 <T> 到java.lang.Class <T> - Cannot convert from java.lang.Class<T> to java.lang.Class<T> 不兼容的类型:java.lang.Class<capture#1 of ?> 无法转换为 - Incompatible types: java.lang.Class<capture#1 of ?> cannot be converted to class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized - class java.lang.Class cannot be cast to class java.lang.reflect.Parameterized java.lang.ClassCastException:无法将java.lang.Class强制转换为java.lang.reflect.ParameterizedType - java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM