Java 5.0 ·ºÐÍÖ® ʹÓ÷ºÐÍͳһ´«ÈëµÄ²ÎÊýÀàÐÍ
package Demo;
// ʹÓ÷ºÐÍͳһ´«ÈëµÄ²ÎÊýÀàÐÍ
class Info28<T> {
private T var; // ´ËÀàÐÍÓÉÍⲿ¾ö¶¨
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
public String toString() { // ¸²Ð´ObjectÀàÖеÄtoString()·½·¨
return this.var.toString();
}
}
public class GenericsDemo28 {
public static void main(String args[]) {
Info28<String> i1 = new Info28<String>();
Info28<String> i2 = new Info28<String>();
i1.setVar("99"); // ÉèÖÃÄÚÈÝ
i2.setVar("it"); // ÉèÖÃÄÚÈÝ
add(i1, i2);
}
public static <T> void add(Info28<T> i1, Info28<T> i2) {
System.out.println(" Key Of String : " + i1.getVar()
+ " / Value Of String : " + i2.getVar());
}
}
Ïà¹ØÎĵµ£º
ÔÚ½ñÄê11Ô·ݣ¬ºêÌìÈí¼þ¿ª·ÅÁ˹«Ë¾µÄ¿ªÔ´°æOA 1.2
beta°æ±¾£¬µÃµ½Í¬ÐÐÒÔ¼°ÆóÒµµÄÈÈÁÒÏìÓ¦£¬¹«Ë¾Ò²¼Ó½ôÁËÏÂÒ»²½µÄ¿ªÔ´µÄ²½·¥£¬ºêÌìÈí¼þ°ÑÏÖÐÐµÄÆóÒµ°æ1.2.1Ò²¶ÔËùÓÐÆóÒµ¿ª·ÅÔ´´úÂ루ÔÝʱ²»¶Ô¸öÈË¿ª
·Å£¬ËäȻĿǰÓкܶà¸öÈ˼°Èí¼þ¿ª·¢ÊÒÇ¿ÁÒÒªÇ󣩣¬ÆóÒµÖ»ÐèÒª»¨ÉÏÍòÔª×óÓÒ£¬¾Í¿ÉÒÔ¹ºÂò¸Ã°æ±¾µÄÈ«²¿´úÂ룬²¢»ñµÃÊÚȨ»ùÓÚ´ËÏ ......
ÔÚ³õʼ»¯Ò»¸öÀ࣬Éú³ÉÒ»¸öʵÀýµÄʱºò£¬newInstance()·½·¨ºÍnew¹Ø¼ü×Ö³ýÁËÒ»¸öÊÇ·½·¨£¬Ò»¸öÊǹؼü×ÖÍ⣬×îÖ÷ÒªÓÐÊ²Ã´Çø±ð£¿ËüÃǵÄÇø±ðÔÚÓÚ´´½¨¶ÔÏóµÄ·½Ê½²»Ò»Ñù£¬Ç°ÕßÊÇʹÓÃÀà¼ÓÔØ»úÖÆ£¬ºóÕßÊÇ´´½¨Ò»¸öÐÂÀà¡£ÄÇôΪʲô»áÓÐÁ½ÖÖ´´½¨¶ÔÏó·½Ê½£¿ÕâÖ÷Òª¿¼Âǵ½Èí¼þµÄ¿ÉÉìËõ¡¢¿ÉÀ©Õ¹ºÍ¿ÉÖØÓõÈÈí¼þÉè¼ÆË¼Ïë¡£
......
package Demo;
// ÊÜÏÞ·ºÐÍ
class Info17<T> {
private T var; // ¶¨Òå·ºÐͱäÁ¿
public void setVar(T var) {
this.var = var;
}
public T getVar() {
return this.var;
}
public String toString() { // Ö±½Ó´òÓ¡
return this.var.toString();
}
}
public class GenericsDemo17 {
publ ......
package Demo;
// ͨ¹ý·ºÐÍ·½·¨·µ»Ø·ºÐÍÀàÐÍ
class Info27<T extends Number> { // Ö¸¶¨ÉÏÏÞ£¬Ö»ÄÜÊÇÊý×ÖÀàÐÍ
private T var; // ´ËÀàÐÍÓÉÍⲿ¾ö¶¨
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
public String toString() { // ¸²Ð´ObjectÀàÖеÄtoStr ......