??????Java??????
?????TellH ???????[ 2017/5/10 9:35:54 ] ??????????????????? Java ????
??????????? ?????????? ?????????????????????????????????????????????????????????JDK 1.5???????????????Java???????????C++??C#???????????д???????????????·???ж??????????????????????Java????????????tricky????????????????????????????????????????????????й?Java?????????????
????????????Э??
????????Э??????????
????class Fruit{}
????class Apple extends Fruit{}
????Fruit[] fruit = new Apple[10]; // OK
??????????????????????????????á?????????????????Э????
????ArrayList<Fruit> flist = new ArrayList<Apple>(); // ??????????
?????????????????????????
????ArrayList<? extends Fruit> flist = new ArrayList<Apple>();// ??????????Э??????
????????
???????????
????List<? extends Fruit> flist = Arrays.asList(new Apple());
????Apple a = (Apple)flist.get(0); // No warning
????flist.contains(new Apple()); // Argument is ‘Object’
????flist.indexOf(new Apple()); // Argument is ‘Object’
????//flist.add(new Apple()); ???????
????List<? extends Fruit> ????????????? ( Fruit ?????????? ) ?? List??????????????????????????????????????????????????????????????????ζ?????List???????Fruit???????????
????????List????????????????????????Java??????????Э?????????з???????????????????????????á????? add(T item); ????????Object???????????
???????????????????????????????? T get(int index); ?????????????????????????????????????????? flist.get(0) ???????Fruit??????
???????????
????static void add(List<? super Apple> list) {
????// list.add(new Fruit()); // ???????
????Object object = list.get(0);// pass
????}
?????????е?
????List<? super Apple> list ????list???е???????Apple????????????????????????????????ζlist???????Apple??????????????????????????????list??????????????????? list.add(new Fruit()); ??????????
???????????
????List<?> list ??? list ???????????????? List????????????????????????????????? List list ???????д???????????????? list ???е??????????? Object ??
???????з??????????????????
??????ν???????????????????Code?????е?????????????????????????в????????????????????????????????ε????????л????????????????
???????? R?? ??????
????λ????????????????д????????????????????
????λ???????????????д???????????????
????public class GenericClass<T> { // 1
????private List<T> list; // 2
????private Map<String?? T> map; // 3
????public <U> U genericMethod(Map<T?? U> m) { // 4
????List<String> list = new ArrayList<>(); // 5
????return null;
????}
????}
?????????????У????1?????4??T??U???????Class??????е????????????????????????????????????????????????????????????????T??U??????????
??????????????????????????????????????????????????????????5?е?list?????????????????????????????
?????????????????????????
????????????????????????????????????????????????????????????????е??????????????????????????????????????????????????????????????????????????JDK1.5???Class?????б仯?????????????????????????????????????????????С?????????????Щ???????????????????????????????????????????????????и?????????????????
???????磺
????public class SubClass extends Base<String> { }
???????Base???????????????????????????SubClass.class???? getGenericSuperclass ????????T???????????
????Type type = SubClass.class.getGenericSuperclass();
????Type targ = ((ParameterizedType) type).getActualTypeArguments()[0];
????System.out.println(type); // SubClass<java.lang.String>
????System.out.println(targ); // class java.lang.String
??????????÷?????ο?Gson??Guice?????
????https://github.com/google/guice/blob/abc78c361d9018da211690b673accb580a52abf2/core/src/com/google/inject/TypeLiteral.java#L94
????https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/%24Gson%24Types.java
?????????
????????Java????????????????????1.5??????????????????????????????????????????壬???????????????????????????
??????????? Method.isBridge() ???????ж?????????????????????????????????????????? ACC_BRIDGE ?? ACC_SYNTHETIC ??
public class Fruit<T> {
T value;
public T getValue() {
return value;
}
}
public class Apple extends Fruit<String> {
@Override
public String getValue() {
return "foo was call";
}
}
????????????????????
public class Apple extends Fruit<java.lang.String> {
public Apple();
Code:
0: aload_0
1: invokespecial #1 // Method Fruit."<init>":()V
4: return
public java.lang.String getValue();
Code:
0: ldc #2 // String calling
2: areturn
public java.lang.Object getValue();
Code:
0: aload_0
1: invokevirtual #3 // Method getValue:()Ljava/lang/String;
4: areturn
}
?????????????????????????????????????????????????????Object??????????????????????????getValue??????
??????Java?????У????????????????????????????????????????????????????е?????????????????????????????????????????? public Object getValue() ?? public String getValue() ??????JVM???????????Class????й?????
??????????????????????????????д????????????????滻??Object??????????????д??????????????????????????????????д????????????????????????????????????????????????????????Apple?е?????? public Object getValue() ???override????Fruit?? public Object getValue() ??????????????????????Apple?? public String getValue() ??????Java????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11