/**
    * @author Rollen-Holt 使用泛型
    */
  1. class Test<T, V> {  
  2.     private T name;  
  3.     private V age;  
  4.     Test() {  
  5.     }  
  6.     public T getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(T name) {  
  10.         this.name = name;  
  11.     }  
  12.     public V getAge() {  
  13.         return age;  
  14.     }  
  15.     public void setAge(V age) {  
  16.         this.age = age;  
  17.     }     
  18.     public static void main(String[] args) {  
  19.         Test<String, Integer> he = new Test<String, Integer>();  
  20.         he.setAge(10);  
  21.         he.setName("Rollen Holt");  
  22.         System.out.println(he.getName() + ":" + he.getAge());  
  23.     }  
   
   
   
   
  1. /**  
  2.  *   
  3.  * @author Rollen-Holt 泛型类的构造方法定义  
  4.  */  
  5. class Test<T, V> {  
  6.     private T name;  
  7.     private V age;  
  8.     Test(T name, V age) {  
  9.         this.age = age;  
  10.         this.name = name;  
  11.     }  
  12.     public T getName() {  
  13.         return name;  
  14.     }  
  15.     public V getAge() {  
  16.         return age;  
  17.     }     
  18.     public static void main(String[] args) {  
  19.         Test<String, Integer> he = new Test<String, Integer>("Rollen", 12);  
  20.         System.out.println(he.getName() + "  " + he.getAge());  
  21.     }  
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 使用通配符  
  3.  */  
  4. class info<T> {  
  5.     private T name;  
  6.     info(T name) {  
  7.         this.name = name;  
  8.     }  
  9. }  
  10. class Test {  
  11.     public static void function(info> temp) {  
  12.         System.out.println("内容: " + temp);  
  13.     }  
  14.     public static void main(String[] args) {  
  15.         info<String> demo = new info<String>("Rollen");  
  16.         function(demo);  
  17.     }  
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 泛型上限  
  3.  */  
  4. class info<T> {  
  5.     info(T age) {  
  6.         this.age = age;  
  7.     }  
  8.     private T age;  
  9. }  
  10. class Test {  
  11.     public static void function(info extends Number> temp) {  
  12.         System.out.println("内容" + temp);  
  13.     }  
  14.     public static void main(String[] args) {  
  15.         info<Integer> demo = new info<Integer>(1);  
  16.         function(demo);  
  17.     }  
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 泛型下限  
  3.  */  
  4. class info<T>{  
  5.     info(T age){  
  6.         this.age=age;  
  7.     }  
  8.     private T age;  
  9. }  
  10. class hello{  
  11.     public static void function(info super String> temp){  
  12.         System.out.println("内容"+ temp);  
  13.     }     
  14.     public static void main(String[] args){       
  15.      // 此处只能使用String 或者Object  
  16.       info<String> demo=new info<String>("Rollen");  
  17.       function(demo);  
  18.     }  

 

    /**
  1.  * @author Rollen-Holt 泛型和子类继承的限制  
  2.  */  
  3. class info<T>{  
  4. }  
  5. class hello{  
  6.     public static void main(String[] args){  
  7.     info<String> demo1=new info<String>();  
  8.     info<Object> demo2=new info<Object>();  
  9.     //demo2=demo1;   此处错误       
  10.     }  
  11. }  
  12. /**  
  13.  * 上面的例子说明,一个类的子类可以通过多态性被其父类实例化  
  14.  * 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。  
  15.  */  
  16. 如果允许上面的条语句的话,会出现:  
  17. Exception in thread "main" java.lang.Error: Unresolved compilation problem:   
  18.     Type mismatch: cannot convert from info<String> to info<Object> 
  19.     at hello.main(hello.java:12) 
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 泛型接口的实现1  
  3.  */  
  4. interface info<T> {  
  5.     public void say();  
  6. }  
  7. // 直接在子类之后声明泛型  
  8.  
  9. class hello<T> implements info<T>{  
  10.     public static void main(String[] args){  
  11.         info<String> demo = new hello<String>();  
  12.         demo.say();  
  13.     }  
  14.     public void say(){  
  15.         System.out.println("hello");  
  16.     }  
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 泛型接口的实现2  
  3.  */  
  4. interface info<T> {  
  5.     public void say();  
  6. }  
  7. // 在子类实现的接口中明确给出泛型类型  
  8. class hello implements info<String>{  
  9.     public static void main(String[] args){  
  10.         info<String> demo = new hello();  
  11.         demo.say();  
  12.     }  
  13.     public void say(){  
  14.         System.out.println("hello");  
  15.     }  
   
   
   
   
  1. /**  
  2.  * @author Rollen-Holt 使用泛型统一传入参数的类型  
  3.  */  
  4. class info<T> {  
  5.     private T var;  
  6.     public T getVar(){  
  7.         return var;  
  8.     }  
  9.     public void setVar(T var){  
  10.         this.var = var;  
  11.     }  
  12.     public String toString(){  
  13.         return this.var.toString();  
  14.     }  
  15. }  
  16. class hello{  
  17.     public static void main(String[] args){  
  18.         info<String> demo1=new info<String>();  
  19.         info<String> demo2=new info<String>();  
  20.         demo1.setVar("Rollen");  
  21.         demo2.setVar("Holt");  
  22.         printAdd(demo1, demo2);  
  23.     }  
  24.     // 此处传递的都是同一个String类型的  
  25.     public static <T> void printAdd(info<T> demo1, info<T> demo2){  
  26.         System.out.println(demo1.getVar()+" "+demo2.getVar());  
  27.     }