当前位置: 首页 > 编程语言 > Java > 正文

J2SE 1.5 Tiger新特性学习

时间:2011-01-31

一、定义一个简单的“泛型”类

package tigers;
public class GenericIdentify {
public static void main(String[] args) {
Generic1 gStr = new Generic1();
gStr.set("aaaaaaaaaaaaaaaaa");
Generic1 gInt = new Generic1();
gInt.set(new Integer(1321423));
Generic1 gBool = new Generic1();
gBool.set(new Boolean(true));
System.out.println(gStr.get() + "\n" +
gInt.get() + "\n" +
gBool.get() + "\n");
((Generic1)gInt).set("bbbbbbbbbbbbb"); //避开类型检查
System.out.println(gInt.get());
}
}
class Generic1 { //声明泛型
private A a;
public void set(A a) {
this.a = a;
}
public A get() {
return this.a;
}
}

C:\tiger>javac -d c:\tiger\cls\ c:\tiger\src\*.java

Note: c:\tiger\src\GenericIdentify.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

C:\tiger>java -classpath c:\tiger\cls tigers.GenericIdentify

aaaaaaaaaaaaaaaaa

1321423

true

bbbbbbbbbbbbb

C:\tiger>