This question already has answers here:
Difference between <? super T> and <? extends T> in Java [duplicate]
                                
                                    (14个回答)
                                
                        
                                3年前关闭。
            
                    
我一直在阅读Sybex的Java 8 OCP书,但我似乎不明白为什么第三行不编译。

public static void main(String[] args) {
    List<? super IOException> exceptions = new ArrayList<Exception>();
     exceptions.add(new Exception()); // DOES NOT COMPILE
     exceptions.add(new IOException());
     exceptions.add(new FileNotFoundException());
}

最佳答案

Exception不继承自IOException,而IOExceptionFileNotFoundException继承。

所以hierarchy是:

Exception
   IOException
       FileNotFoundException


因此,FileNotFoundExceptionIOException,但是Exception不是。

关于java - 具有较低界通配符的Java泛型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37560457/

10-11 21:59
查看更多