????????????????????????????
????class TObject
????{
????privateObject obj;
????publicvoid Set(Object object)
????{
????this.obj= object;
????}
????}
??????????????T???????滻?????????Object????????????T?????
??????3????????й???????????????
????class Manipulator<Textends SuperClass>
????{
????private T obj;
????public Manipulator(T x){
????obj = x;
????}
????public void doSomething(){
????obj.f();
????System.out.println(obj.getClass().getName());
????}
????}
????????????????T?????滻????磬???????????????
????class Manipulator
????{
????private SuperClass obj;
????public Manipulator(SuperClass x){
????obj = x;
????}
????public void doSomething(){
????obj.f();
????System.out.println(obj.getClass().getName());
????}
????}
???????????????
?????????????java?????е????????java?з????????????????Ρ?
????????????????????÷??????????????????
??????1?????????????????д????????????????????????????????????????
??????2?????????????????????????????????????????????????????÷?????????????????
????5????Э??
??????1????????????
???????????Э?????????????Э????
?????????Э?????????????????
?????????Э??(covariant)????????Base????Sub????????Base[]??Sub[]?????
?????????????(invariant)???List<Base>??????List<Sub>??????????????????
??????2?????????????Э??
????????“??????????????饗????????”?????????????Э??
????ArrayList<Object> objList = new ArrayList<Long>();
????//can't compile pass
?????????????
????Object[] objArray = new Long[10];
????//compile OK
???????ArrayList<Long>??????????????ArrayList<Object>????????????Υ??????????????????
????????????????????????????????????????з??÷?Long??????????????????ν?????????????????
????????????????????б???????????????Э???????????????п??????
????List<Type> listt = new ArrayList<SubType>();
????//can't compile pass
????List<? extends Type> listt = new ArrayList<SubType>();
????//OK
?????ο?????????????????????
????????Э????????????????????н????????????????????棬????????????????
????ArrayList<Type> list = new ArrayList<Type>();
????//Type is SuperClass
????list.add(new SubType());
????//SubType is SubClass
????Type[] tt = new Type[3];
????tt[0] = new SubType();
??????3???????Э???????????
?????????Э???????????Щ????????????????
????public static voidmain(String[] args) {
????Object[] array = new String[10];
????array[0] = 10;
????}
?????????????????????????????Э????Object[]??????????????????String[]???????????????е??????????????????
????Exception in thread"main" java.lang.ArrayStoreException: java.lang.Integer
??????????????????????????????
????public static voidmain(String[] args) {
????List< Object> list = newArrayList< String>();
????list.add(10);
????}
??????δ??????????????????
????6.??????
?????????????????е????ò?????????????Э??covariant??????????????????????????????????????Э????????
??????????????????List<?>??List<AnyType>?????List<? extends  Type>??List<SubType>?????
// collection1???????κ?????
Collection<?>collection1 = new ArrayList<String>();
collection1 = newArrayList<Integer>();
collection1 = newArrayList<Object>();
//collection3???????????Number??Number??????
Collection<?extends Number> collection3 = null;
collection3 = newArrayList<Number>();
collection3 = newArrayList<Double>();
collection3 = newArrayList<Long>();
//collection4???????????Integer??Integer?????
Collection<? superInteger> collection4 = null;
collection4 = newArrayList<Object>();