Welcome 微信登录

首页 / 软件开发 / JAVA / J2SE 1.5 Tiger新特性学习

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() + " " +
gInt.get() + " " +
gBool.get() + " ");
((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: iger>javac -d c: igercls c: igersrc*.java

Note: c: igersrcGenericIdentify.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

C: iger>java -classpath c: igercls tigers.GenericIdentify

aaaaaaaaaaaaaaaaa

1321423

true

bbbbbbbbbbbbb

C: iger>