Why can't Wild Cards be used in declaring a universal class and method?

Announcement:

  class A<X extends Number & List> {  } 

allowed. If such an announcement is not permitted.

  class A<? extends Number & List> {  }

Is there a logical explanation for why Java limits us to this?

& what is the actual difference between

      <T extends Number>  
     & <? extends Number>?
0
source share
3 answers

The whole point of a type parameter, such as T, is that you can use it as a type inside a class. What does a wildcard mean? If you cannot use it anywhere, why do you have a type parameter?

+1
source

<? extends Number & List>, type. .

, ? extends Number , , , .

+3

, T U.? , , :

class Foo<T extends Number & List> {
    void doStuff(List<T> items) {
        // ...
    }

    void doMoreStuff(List<? extends OutputStream> streams) {
        // ...
    }
}

doStuff() , List<T>, T Foo. :

class Weird extends Number implements List {
    //
}

Foo<Weird> f = new Foo<Weird>();
f.doStuff(...);   // wants a List<Weird>

doMoreStuff() f, - List<OutputStream>, List<FilterOutputStream>, List<ByteArrayOutputStream> ..

0

Source: https://habr.com/ru/post/1548369/


All Articles