各位前辈,国内翻墙不易,真诚请教前辈们offer选校建议,谢谢你们# ChemEng - 化学工程
x*n
1 楼
请问下面一段代码中,为什么第8行处能compile通过而第14行不行?
我的理解是,exceptions 事实上是List (因此所有Exception的子类都可
以包含),而numbers事实上就是List,那么14行也理应可以通过才对?
1 ArrayList < ? super IOException > exceptions =
2 new ArrayList < Exception > ();
3 IOException e1 = new IOException("Problem 1");
4 IOException e2 = new IOException("Problem 2");
5 FileNotFoundException e3 = new FileNotFoundException("Problem 3");
6 exceptions.add(e1);
7 exceptions.add(e2);
8 exceptions.add(e3);
9 System.out.println(exceptions);
10
11 List numbers = new ArrayList ();
12 numbers.add(232);
13 Number n = new Integer(2);
14 numbers.add(n); //can't compile
非常感谢!
我的理解是,exceptions 事实上是List
以包含),而numbers事实上就是List
1 ArrayList < ? super IOException > exceptions =
2 new ArrayList < Exception > ();
3 IOException e1 = new IOException("Problem 1");
4 IOException e2 = new IOException("Problem 2");
5 FileNotFoundException e3 = new FileNotFoundException("Problem 3");
6 exceptions.add(e1);
7 exceptions.add(e2);
8 exceptions.add(e3);
9 System.out.println(exceptions);
10
11 List numbers = new ArrayList
12 numbers.add(232);
13 Number n = new Integer(2);
14 numbers.add(n); //can't compile
非常感谢!