1、泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。

1、泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。

2、为了让不同方法可以操作不同类型,而且类型还不确定。那么可以将泛型定义在方法上。

3、特殊之处:

静态方法不可以访问类上定义的泛型。

如果静态方法操作的应用数据类型不确定,可以将泛型定义在方法上。

[java]  view plain  copy
  1. package tan;  
  2. //定义泛型类(要操作的类型不确定)  
  3. class Demo<T>{  
  4.     public void show(T t){  
  5.         System.out.println("show:"+t);  
  6.     }  
  7.     //方法中定义泛型  
  8.     public <Q> void print(Q q){  
  9.         System.out.println("pirnt:"+q);  
  10.           
  11.     }  
  12.     //静态方法中定义泛型  
  13.     public static <G> void method(G g){  
  14.         System.out.println("mentod:"+g);  
  15.     }  
  16. }  
  17. public class GenericDemo1 {  
  18.     public static void main(String[] args) {  
  19.         Demo<String> d=new Demo<String>();  
  20.                     d.show("beijing");  
  21.                   //d.show(6); 编译失败,因为show方法会随着对象的类型,对象是什么类型的,show方法中就只能接受什么类型  
  22.                     d.print(888);//将泛型定义在方法上传什么类型都可以  
  23.                     d.print("tan");  
  24.                     //类名调用静态方法  
  25.                     Demo.method("IT Dreamer");  
  26.           
  27.           
  28.           
  29.           
  30.     }  
  31. }  


4、将泛型定义在接口上

[java]  view plain  copy
  1. package tan;  
  2. //将泛型定义在接口上  
  3. interface Inter<T>{  
  4.     public void show(T t);  
  5. }  
  6. //确定操作类型  
  7. /*class InterImpl implements Inter<String>{ 
  8.     @Override 
  9.     public void show(String t) { 
  10.         System.out.println("show-----"+t); 
  11.          
  12.     } 
  13. }*/  
  14. //方式二:实现接口以后仍然不知操作类型时  
  15. class InterImpl<T> implements Inter<T>{  
  16.     @Override  
  17.     public void show(T t) {  
  18.         System.out.println("show-----"+t);  
  19.           
  20.     }  
  21. }  
  22. public class GenericDemo2 {  
  23.     public static void main(String[] args) {  
  24.         /*InterImpl impl=new InterImpl(); 
  25.         impl.show("Great");*/  
  26.           
  27.         InterImpl<Integer> i = new InterImpl<Integer>();  
  28.         i.show(4);  
  29.           
  30.     }  
  31. }  

5、? 通配符。也可以理解为占位符。

[java]  view plain  copy
  1. package tan;  
  2. import java.util.*;  
  3. public class GenericDemo3 {  
  4.     public static void main(String[] args) {  
  5.         ArrayList<String> al = new ArrayList<String>();  
  6.   
  7.         al.add("abc1");  
  8.         al.add("abc2");  
  9.         al.add("abc3");  
  10.   
  11.         ArrayList<Integer> al1 = new ArrayList<Integer>();  
  12.         al1.add(4);  
  13.         al1.add(7);  
  14.         al1.add(1);  
  15.           
  16.         printColl(al);  
  17.         printColl(al1);  
  18.               
  19.     }  
  20.     //1、不明确类型用占位符表示  
  21.     public static void printColl(ArrayList<?> al){  
  22.         Iterator<?> it = al.iterator();  
  23.         while(it.hasNext())  
  24.         {  
  25.             System.out.println(it.next().toString());  
  26.         }  
  27.     }     
  28.     //2、也可以用下面这种方式:如果T是一个具体类型的话,可以接收并操作具体类型  
  29.     public static <T>void printColl2(ArrayList<T> al){  
  30.         Iterator<T> it = al.iterator();  
  31.         while(it.hasNext())  
  32.         {  
  33.             T t=it.next();  
  34.             System.out.println(t);  
  35.         }  
  36.     }     
  37. }  

6、泛型限定

? extends E: 可以接收E类型或者E的子类型。上限。
? super E: 可以接收E类型或者E的父类型。   下限


上限示例

[java]  view plain  copy
  1. package xiao;  
  2. import java.util.*;  
  3. public class GenericDemo3 {  
  4.     public static void main(String[] args) {  
  5.         ArrayList<Person> al = new ArrayList<Person>();  
  6.         al.add(new Person("tan11"));  
  7.         al.add(new Person("tan13"));  
  8.         al.add(new Person("tan21"));  
  9.         ArrayList<Student> al1 = new ArrayList<Student>();  
  10.         al1.add(new Student("stu01"));  
  11.         al1.add(new Student("stu02"));  
  12.         al1.add(new Student("stu03"));  
  13.   
  14.         printColl(al);  
  15.         System.out.println();  
  16.         printColl2(al);  
  17.         printColl2(al1);  
  18.   
  19.     }  
  20.   
  21.     // 1、只能打印父类类型  
  22.     public static void printColl(ArrayList<Person> al) {  
  23.         Iterator<Person> it = al.iterator();  
  24.         while (it.hasNext()) {  
  25.             System.out.println(it.next().getName());  
  26.         }  
  27.     }  
  28.   
  29.     // 2.既可以打印父类也可以打印子类类型       《 上限》? extends E: 可以接收E类型或者E的子类型  
  30.     public static void printColl2(ArrayList<? extends Person> al) {  
  31.         Iterator<? extends Person> it = al.iterator();  
  32.         while (it.hasNext()) {  
  33.             System.out.println(it.next().getName());  
  34.         }  
  35.     }  
  36. }  
  37.   
  38. class Person {  
  39.     private String name;  
  40.   
  41.     Person(String name) {  
  42.         this.name = name;  
  43.     }  
  44.   
  45.     public String getName() {  
  46.         return name;  
  47.     }  
  48. }  
  49.   
  50. class Student extends Person {  
  51.     Student(String name) {  
  52.         super(name);  
  53.     }  
  54.   
  55. }  
  56. /* 
  57. class Student implements Comparable<Person>//<? super E> 
  58. { 
  59.     public int compareTo(Person s) 
  60.     { 
  61.         this.getName() 
  62.     }*/  

下限示例:

[java]  view plain  copy
  1. package zheng;  
  2. import java.util.Comparator;  
  3. import java.util.Iterator;  
  4. import java.util.TreeSet;  
  5.   
  6. public class GenericDemo5 {  
  7.     public static void main(String[] args) {  
  8.         TreeSet<Student> ts = new TreeSet<Student>(new Comp());  
  9.         ts.add(new Student("abc1"));  
  10.         ts.add(new Student("abc2"));  
  11.         ts.add(new Student("abc3"));  
  12.         Iterator<Student> it = ts.iterator();  
  13.   
  14.         while(it.hasNext())  
  15.         {  
  16.             System.out.println(it.next().getName());  
  17.         }  
  18.   
  19.     }  
  20. }  
  21. class Person  
  22. {  
  23.     private String name;  
  24.     Person(String name)  
  25.     {  
  26.         this.name = name;  
  27.     }  
  28.     public String getName()  
  29.     {  
  30.         return name;  
  31.     }  
  32. }  
  33.   
  34. class Student extends Person  
  35. {  
  36.     Student(String name)  
  37.     {  
  38.         super(name);  
  39.     }  
  40.   
  41. }  
  42. class Comp implements Comparator<Person>//<? super E>  
  43. {  
  44.     public int compare(Person s1,Person s2)  
  45.     {  
  46.   
  47.         //Person s1 = new Student("abc1");  
  48.         return s1.getName().compareTo(s2.getName());  
  49.     }  
  50. }  

建立通用的比较器方法

[java]  view plain  copy
  1. package Qiang;  
  2. import java.util.*;  
  3. class GenericDemo4  
  4. {  
  5.     public static void main(String[] args)   
  6.     {  
  7.           
  8.         TreeSet<Student> ts = new TreeSet<Student>(new Comp());  
  9.   
  10.         ts.add(new Student("abc03"));  
  11.         ts.add(new Student("abc02"));  
  12.         ts.add(new Student("abc06"));  
  13.         ts.add(new Student("abc01"));  
  14.           
  15.         Iterator<Student> it = ts.iterator();  
  16.   
  17.         while(it.hasNext())  
  18.         {  
  19.             System.out.println(it.next().getName());  
  20.         }  
  21.       
  22.   
  23.         TreeSet<Worker> ts1 = new TreeSet<Worker>(new Comp());  
  24.   
  25.         ts1.add(new Worker("wabc--03"));  
  26.         ts1.add(new Worker("wabc--02"));  
  27.         ts1.add(new Worker("wabc--06"));  
  28.         ts1.add(new Worker("wabc--01"));  
  29.   
  30.   
  31.         Iterator<Worker> it1 = ts1.iterator();  
  32.   
  33.         while(it1.hasNext())  
  34.         {  
  35.             System.out.println(it1.next().getName());  
  36.         }  
  37.     }  
  38. }  
  39. //比较器是通用的,  
  40. class Comp implements Comparator<Person>  
  41. {  
  42.     public int compare(Person p1,Person p2)  
  43.     {           //只能使用父类中的方法,有扩展性就有局限性  
  44.         return p2.getName().compareTo(p1.getName());  
  45.     }  
  46. }  
  47.   
  48.   
  49. class Person  
  50. {  
  51.     private String name;  
  52.     Person(String name)  
  53.     {  
  54.         this.name = name;  
  55.     }  
  56.     public String getName()  
  57.     {  
  58.         return name;  
  59.     }  
  60.     public String toString()  
  61.     {  
  62.         return "person :"+name;  
  63.     }  
  64. }  
  65.   
  66. class Student extends Person  
  67. {  
  68.     Student(String name)  
  69.     {  
  70.         super(name);  
  71.     }  
  72.   
  73. }  
  74.   
  75. class Worker extends Person  
  76. {  
  77.     Worker(String name)  
  78.     {  
  79.         super(name);  
  80.     }  
  81. }  

猜你喜欢

转载自blog.csdn.net/q9104422999/article/details/79297212