import java.util.LinkedList;
/**
* @ Class GenericDemo.java
* @ Description 泛型设计中的通配符
* @ Company OpenData
* @ author Chenlly E-mail: Chenlly99@Gmail.com
* @ Version 1.0
* @ Date 2009-6-17 下午01:44:45
*/
interface Box<T>{
public void put(T element);
public T get(int i);
public int size();
}
class BoxImpl<T> implements Box<T>{
private LinkedList link = new LinkedList();
private int size;
public T get(int i) {
return (T)link.get(i);
}
public void put(T element) {
link.add(element);
size ++;
}
public int size(){
return size;
}
}
public class GenericDemo {
public static void main(String []args){
//字符串
Box<String> box = new BoxImpl<String>();
box.put("chen");
box.put("li");
for( int i = 0; i<box.size(); i++){
System.out.println("->"+box.get(i));
}
System.out.println("*************************");
//整数
Box<Integer> box1 = new BoxImpl<Integer>();
box1.put(new Integer(1000));
box1.put(new Integer(1254));
for( int i = 0; i<box1.size(); i++){
System.out.println("->"+box1.get(i));
}
}
}
后台打印结果
->chen
->li
*************************
->1000
->1254