Java基础学习笔记--常用API之泛型编程

  1 package com.common.api;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Comparator;
  5 import java.util.Iterator;
  6 import java.util.TreeSet;
  7 
  8 /*
  9  *     泛型:JDK1.5 版本以后出现的新特性,用于安全问题,是一个安全机制
 10  *     好处:
 11  *         1、将运行时出现的问题,转移到编译时期,方便程序员解决问题,让运行时问题减少
 12  *         2、避免了强制转换了麻烦
 13  * 
 14  *     泛型格式:
 15  *         通过<>来定义要操作的引用数据类型
 16  * 
 17  *     在使用Java提供的对象时,什么时候使用泛型呢?
 18  *         通常情况下,在集合框架中很常见,只要见到<>就要定义泛型
 19  *         其实<>就是用来接收类型的
 20  *         当使用集合时,将集合中药存储的数据类型作为参数传递到<>中即可
 21  * 
 22  *     什么时候需要定义泛型类?
 23  *         当类中药操作的引用数据类型不确定的时候,可以通过定义泛型类处理
 24  * 
 25  *     泛型类的缺点:
 26  *         泛型类定义的泛型,在整个类中有效,如果被方法使用,那么泛型类的对象明确操作的具体类型后,所有
 27  *         操作的类型就已经固定了
 28  *     
 29  *     泛型类缺点的解决办法:定义泛型方法
 30  * 
 31  *     特殊之处:
 32  *         1、静态方法不可以访问类上定义的泛型
 33  *         2、如果静态方法操作的应用数据类型不确定,可以将泛型定义到方法上
 34  * 
 35  *     泛型的类型:
 36  *         1、泛型类
 37  *         2、类的泛型方法
 38  *         3、类的泛型静态方法
 39  *         4、泛型接口
 40  * 
 41  *     泛型的高级应用:
 42  *         1、? 通配符,也可以理解为占位符
 43  *         2、? extends E 可以接收E类型或者E的子类型,上限
 44  *         3、? super E 可以接收E类型或者E的父类型,下限
 45  */
 46 public class GeniricDemo {
 47     private static void genericTreeSet()
 48     {
 49         TreeSet<String> ts=new TreeSet<String>();
 50         ts.add("abc01");
 51         ts.add("acb02");
 52         ts.add("acb03");
 53         ts.add("cab04");
 54         ts.add("bca05");
 55         
 56         Iterator<String> it=ts.iterator();
 57         
 58         while(it.hasNext())
 59         {
 60             String s=it.next();
 61             System.out.println(s);
 62         }
 63     }
 64     
 65     private static void genericTreeSet2()
 66     {
 67         System.out.println("使用比较器的TreeSet实例......");
 68         TreeSet<String> ts=new TreeSet<String>(new LenComparator());
 69         ts.add("abc010");
 70         ts.add("acb0200");
 71         ts.add("acb030000");
 72         ts.add("cab04");
 73         ts.add("bca050000000");
 74         
 75         Iterator<String> it=ts.iterator();
 76         
 77         while(it.hasNext())
 78         {
 79             String s=it.next();
 80             System.out.println(s);
 81         }
 82     }
 83     private static void genericArrayList() {
 84         ArrayList<String> a1=new ArrayList<String>();
 85         a1.add("java01");
 86         a1.add("java02");
 87         a1.add("java03");
 88         a1.add("java04");
 89         
 90         //a1.add(4); //编译时就会报错,如果不用泛型,即上面使用    
 91                      //ArrayList a=new ArrayList()的方式,则此处在编译的时候不会报错,
 92                      //通过泛型,在编译时报错,从而达到了一种安全性检查
 93         
 94         Iterator<String> it=a1.iterator();
 95         while(it.hasNext())
 96         {
 97             String s=it.next();
 98             System.out.println(s+" : "+s.length());
 99         }
100     }
101     
102     public static void generic_customerize()
103     {
104         Tool t=new Tool();
105         t.setObject(new Worker());
106         t.getObject();
107         t.setObject(new Teacher());
108         t.getObject();
109         
110         Utils<Worker> w=new Utils<Worker>();
111         w.setObject(new Worker());
112         w.getObject();
113         
114         //用于展示泛型方法
115         MethodGenericDemo<String> m=new MethodGenericDemo<String>();
116         m.show("hello world");
117         m.print("hello world");
118         
119         MethodGenericDemo<Integer> m1=new MethodGenericDemo<Integer>();
120         m1.show(100);
121         m1.print(200);
122         
123         // 同一个对象,同一个方法,可以打印不同的类型,泛型在方法层面的应用
124         MethodGenericDemo2 m2=new MethodGenericDemo2();
125         m2.show("hello java");
126         m2.show(1000);
127         m2.print("hello world");
128         m2.print(2000);
129         
130         //同时定义了泛型类和泛型方法的,即可以用泛型类固定方法中的引用数据类型
131         //也可以通过普通类调用不同的引用数据类型的泛型方法
132         //如下 m3实例调用的非泛型方法只能用string类型,而m33可以
133         MethodGenericDemo3<String> m3=new MethodGenericDemo3<String>();
134         m3.show("hello world");
135         // m3.show(new Integer(100)); show 方法未定义泛型,需要功能泛型类的类型保持一致
136         m3.print("hello world");
137         m3.print(100);
138         MethodGenericDemo3 m33=new MethodGenericDemo3();
139         m33.show("hello world");
140         m33.show(200);
141         m33.print("hello world");
142         m33.print(100);
143         
144         //静态方法泛型
145         MethodGenericDemo3.method("hello world");
146         MethodGenericDemo3.method(100);
147         
148         //泛型定义在接口上使用实例
149         InterImpl i=new InterImpl();
150         i.show("hello world");
151         
152         //泛型定义的接口,实现类继续用泛型
153         InterImp2 i2=new InterImp2();
154         i2.show("hello world");
155         i2.show(100);
156     }
157     //类型不确定时可以使用问号?,表示占位符
158     public static void printAll(ArrayList<?> a)
159     {
160         Iterator<?> it=a.iterator();
161         while(it.hasNext())
162         {
163             System.out.println(it.next());
164         }
165     }
166     
167     //泛型限定
168     public static void printAllP(ArrayList<? extends PPerson> a)
169     {
170         Iterator<?> it=a.iterator();
171         while(it.hasNext())
172         {
173             System.out.println(it.next());
174         }
175     }
176     public static void method_advance()
177     {
178         ArrayList<String> a1=new ArrayList<String>();
179         a1.add("abc01");
180         a1.add("abc02");
181         a1.add("abc03");
182         
183         ArrayList<Integer> a2=new ArrayList<Integer>();
184         a2.add(100);
185         a2.add(200);
186         a2.add(300);
187         
188         printAll(a1);
189         printAll(a2);
190         
191         ArrayList<PPerson> p1=new ArrayList<PPerson>();
192         p1.add(new PPerson("abc"));
193         p1.add(new PPerson("abcd"));
194         p1.add(new PPerson("abcde"));
195         
196         printAllP(p1);
197         
198         
199         
200     }
201     public static void main(String[] args) {
202         // TODO Auto-generated method stub
203         genericArrayList();
204         
205         genericTreeSet();
206         
207         genericTreeSet2();
208         
209         generic_customerize();
210         
211         method_advance();
212     }
213 }
214 
215 class LenComparator implements Comparator<String>
216 {
217     public int compare(String o1,String o2)
218     {
219         int num=new Integer(o1.length()).compareTo(new Integer(o2.length()));
220         if(num==0)
221         {
222             return o1.compareTo(o2);
223         }
224         return num;
225     }
226 }
227 
228 class Worker
229 {
230     
231 }
232 class Teacher
233 {
234     
235 }
236 //泛型前的处理方法
237 class Tool
238 {
239     private Object obj;
240     public void setObject(Object obj)
241     {
242         this.obj=obj;
243     }
244     public Object getObject()
245     {
246         return this.obj;
247     }
248 }
249 
250 // 泛型处理方法:定义泛型类
251 class Utils<T>
252 {
253     private T t;
254     
255     public void setObject(T t)
256     {
257         this.t=t;
258     }
259     public T getObject()
260     {
261         return this.t;
262     }
263 }
264 
265 // 用于演示定义泛型类的方法
266 class MethodGenericDemo<T>
267 {
268     public void show(T t)
269     {
270         System.out.println("show:"+t);
271     }
272     public void print(T t)
273     {
274         System.out.println("print:"+t);
275     }
276 }
277 
278 //用于演示泛型方法
279 class MethodGenericDemo2
280 {
281     public <T> void show(T t)
282     {
283         System.out.println("show:"+t);
284     }
285     
286     public <T> void print(T t)
287     {
288         System.out.println("print:"+t);
289     }
290 }
291 
292 //同时泛型类和泛型方法
293 class MethodGenericDemo3<T>
294 {
295     public void show(T t)
296     {
297         System.out.println("show:"+t);
298     }
299     
300     public <T> void print(T t)
301     {
302         System.out.println("print:"+t);
303     }
304     
305     //静态方法不能访问类中的泛型
306 //    public static void method(T t)
307 //    {
308 //        System.out.println("method: "+t);
309 //    }
310     //如果静态方法引用数据不确定,需要将泛型定义在方法上
311     public static <T> void method(T t)
312     {
313         System.out.println("method :"+t);
314     }
315 }
316 
317 //    泛型定义在接口上
318 interface Inter<T>
319 {
320     void show(T t);
321 }
322 
323 //实现接口的时候使用具体数据类型
324 class InterImpl implements Inter<String>
325 {
326     public void show(String t)
327     {
328         System.out.println("show: "+t);
329     }
330 }
331 
332 //实现接口的时候仍然使用泛型
333 class InterImp2<T> implements Inter<T>
334 {
335     public void show(T t)
336     {
337         System.out.println("show :"+t);
338     }
339 }
340 
341 class PPerson
342 {
343     private String name;
344     PPerson(String name)
345     {
346         this.name=name;
347     }
348     public String getName()
349     {
350         return this.name;
351     }
352 }

执行结果为:

java01 : 6
java02 : 6
java03 : 6
java04 : 6
abc01
acb02
acb03
bca05
cab04
使用比较器的TreeSet实例......
cab04
abc010
acb0200
acb030000
bca050000000
show:hello world
print:hello world
show:100
print:200
show:hello java
show:1000
print:hello world
print:2000
show:hello world
print:hello world
print:100
show:hello world
show:200
print:hello world
print:100
method :hello world
method :100
show: hello world
show :hello world
show :100
abc01
abc02
abc03
100
200
300
com.common.api.PPerson@15db9742
com.common.api.PPerson@6d06d69c
com.common.api.PPerson@7852e922

  

猜你喜欢

转载自www.cnblogs.com/redrose2100/p/12450092.html
今日推荐