简体   繁体   English

泛型-不兼容的类型

[英]Generics - incompatible types

public class HelloWorld{

public static void main(String[] args) {

    List<? extends Number> callback = new ArrayList<Long>();
    new Container<>().test(callback);
}

public static class Container<T extends Number> {

    public void test(List<T> some) {

    }

}

} }

this code produces 该代码产生

HelloWorld.java:7: error: incompatible types: List<CAP#1> cannot be converted to List<Number>                                                                                                                                                          
        new Container<>().test(callback);                                                                                                                                                                                                              
                               ^                                                                                                                                                                                                                       
  where CAP#1 is a fresh type-variable:                                                                                                                                                                                                                
    CAP#1 extends Number from capture of ? extends Number  

Can you explain in details this code incorrect. 您能否详细说明此代码不正确。

I expect that new Container will be generalized with type compatible with callback 我希望新的Container将被泛化为与回调兼容的类型

Firstly, there cannot be a type that extends String as String is a final class. 首先,不能存在扩展String的类型,因为String是最终类。

Secondly, the compiler cannot determine if the type ? extends SomeType 其次,编译器无法确定类型是否为? extends SomeType ? extends SomeType that you use for your List is the same type as T extends SomeType that you use for the Container class, so it produces an error. ? extends SomeType你用你的List是相同类型的T extends SomeType您使用的Container类,所以它产生一个错误。

What you would need to do is either declare a generic type in the method signature, and use that type for both the List and Container : 您需要做的就是在方法签名中声明一个泛型类型,然后对ListContainer使用该类型:

public <T extends SomeClass> void example() {
    List<T> callback = new ArrayList<>();
    new Container<T>().test(callback);
}

Or declare the list with a non-bounded type: 或使用不受限制的类型声明列表:

List<SomeClass> callback = new ArrayList<>();
new Container<>().test(callback);

Well, you're using generics. 好吧,您正在使用泛型。 as you define Container, it tells to Container default constructor must support a type which is instance of String. 当您定义Container时,它告诉Container默认构造函数必须支持类型为String的实例。 here is a correct example which String is subClass of Object, so i use that. 这是一个正确的示例,其中String是Object的子类,所以我使用它。

public class HelloWorld{

    public static void main(String[] args) {

        List<String> callback = new ArrayList<String>();
        new Container<String>().test(callback);
    }

    public static class Container<T extends Object> {

        public void test(List<T> some) {

        }

    }
}

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM