下载通配符的Java泛型

时间:2016-06-01 05:53:50

标签: java generics

我一直在阅读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());
}

1 个答案:

答案 0 :(得分:1)

ExceptionIOExceptionIOException的情况下不会从FileNotFoundException继承。

所以hierarchy是:

Exception
   IOException
       FileNotFoundException

因此,FileNotFoundExceptionIOException,但Exception不是。