Java泛型綁定類型擦除
Java編譯器用通用類型的類型參數替換其綁定的有界類型參數。
示例
創建一個名稱爲:BoundTypesErasure.java 文件,並編寫以下代碼 -
package com.yiibai.demo;
public class BoundTypesErasure {
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
Box<Double> doubleBox = new Box<Double>();
integerBox.add(new Integer(130));
doubleBox.add(new Double(134.99));
System.out.printf("Integer Value :%d\n", integerBox.get());
System.out.printf("Double Value :%s\n", doubleBox.get());
}
}
class Box<T extends Number> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
}
執行上面代碼,得到以下結果 -
Integer Value :130
Double Value :134.99