Is it possible to specify both upper and lower bound constraints on type parameters in Java?

后端 未结 3 601
南旧
南旧 2020-12-09 10:12

Is it possible to specify both upper and lower bound constraints on type parameters in Java?

I found a conversation in Sun\'s forum in which this issue was discussed

3条回答
  •  有刺的猬
    2020-12-09 10:41

    I don't believe so - as far as I can tell from the language specification, "super" is only valid for wildcard types in the first place. The syntax for wildcards also suggests you can only have one wildcard bound, too - so you can't use something like this either:

    // Invalid
    void foo(List list)
    

    Even though both of these are okay:

    // Valid
    void foo(List list)
    
    // Valid
    void foo(List list)
    

    As noted in comments, it's possible to have multiple upper bounds - but only for type parameters and cast expressions. For example:

    // Valid
     void foo(List list)
    

提交回复
热议问题