简体   繁体   中英

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

Firstly, there cannot be a type that extends String as String is a final class.

Secondly, the compiler cannot determine if the type ? 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.

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 :

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. here is a correct example which String is subClass of Object, so i use that.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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