如果类型参数在创建期间传递了npt,则原始类型是泛型类或接口的对象.下面的例子将展示上面提到的概念.
示例
使用您选择的任何编辑器创建以下java程序.
GenericsTester.java
package com.it1352; public class GenericsTester { public static void main(String[] args) { Boxbox = new Box (); box.set(Integer.valueOf(10)); System.out.printf("Integer Value :%d\n", box.getData()); Box rawBox = new Box(); //No warning rawBox = box; System.out.printf("Integer Value :%d\n", rawBox.getData()); //Warning for unchecked invocation to set(T) rawBox.set(Integer.valueOf(10)); System.out.printf("Integer Value :%d\n", rawBox.getData()); //Warning for unchecked conversion box = rawBox; System.out.printf("Integer Value :%d\n", box.getData()); }}class Box { private T t; public void set(T t) { this.t = t; } public T getData() { return t; } }
这将产生以下结果.
输出
Integer Value :10Integer Value :10Integer Value :10Integer Value :10