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