Skip to content

Instantly share code, notes, and snippets.

@yanglikun
Created September 3, 2017 06:52
Show Gist options
  • Save yanglikun/c0fc06210c62cbb1cf49e9baab570c42 to your computer and use it in GitHub Desktop.
Save yanglikun/c0fc06210c62cbb1cf49e9baab570c42 to your computer and use it in GitHub Desktop.
genericExtendAndSuper
List<? extends Number> extendsList = null;
List<? super Number> superList = null;
List<Object> objList = null;
List<Number> numberList = null;
List<Integer> integerList = null;
//extendsList=objList; //compile error
extendsList = numberList;
extendsList = integerList;
superList = objList;
superList = numberList;
//superList = integerList; //compile error
Object obj = null;
Number num = null;
Integer integer = null;
//extend 适合读取,因为不能添加
//super 适合添加,因为读取出来是Object类型
//extendsList.add(obj); // compile error
//extendsList.add(num);//compile error
//extendsList.add(integer);//compile error
//superList.add(obj);//compile error
superList.add(num);
superList.add(integer);
Number number = extendsList.get(0);
Object object = superList.get(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment