package com.common.api;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/*
* 泛型:JDK1.5 版本以後出現的新特性,用於安全問題,是一個安全機制
* 好處:
* 1、將運行時出現的問題,轉移到編譯時期,方便程序員解決問題,讓運行時問題減少
* 2、避免了強制轉換了麻煩
*
* 泛型格式:
* 通過<>來定義要操作的引用數據類型
*
* 在使用Java提供的對象時,什麼時候使用泛型呢?
* 通常情況下,在集合框架中很常見,只要見到<>就要定義泛型
* 其實<>就是用來接收類型的
* 當使用集合時,將集合中藥存儲的數據類型作爲參數傳遞到<>中即可
*
* 什麼時候需要定義泛型類?
* 當類中藥操作的引用數據類型不確定的時候,可以通過定義泛型類處理
*
* 泛型類的缺點:
* 泛型類定義的泛型,在整個類中有效,如果被方法使用,那麼泛型類的對象明確操作的具體類型後,所有
* 操作的類型就已經固定了
*
* 泛型類缺點的解決辦法:定義泛型方法
*
* 特殊之處:
* 1、靜態方法不可以訪問類上定義的泛型
* 2、如果靜態方法操作的應用數據類型不確定,可以將泛型定義到方法上
*
* 泛型的類型:
* 1、泛型類
* 2、類的泛型方法
* 3、類的泛型靜態方法
* 4、泛型接口
*
* 泛型的高級應用:
* 1、? 通配符,也可以理解爲佔位符
* 2、? extends E 可以接收E類型或者E的子類型,上限
* 3、? super E 可以接收E類型或者E的父類型,下限
*/
public class GeniricDemo {
private static void genericTreeSet()
{
TreeSet<String> ts=new TreeSet<String>();
ts.add("abc01");
ts.add("acb02");
ts.add("acb03");
ts.add("cab04");
ts.add("bca05");
Iterator<String> it=ts.iterator();
while(it.hasNext())
{
String s=it.next();
System.out.println(s);
}
}
private static void genericTreeSet2()
{
System.out.println("使用比較器的TreeSet實例......");
TreeSet<String> ts=new TreeSet<String>(new LenComparator());
ts.add("abc010");
ts.add("acb0200");
ts.add("acb030000");
ts.add("cab04");
ts.add("bca050000000");
Iterator<String> it=ts.iterator();
while(it.hasNext())
{
String s=it.next();
System.out.println(s);
}
}
private static void genericArrayList() {
ArrayList<String> a1=new ArrayList<String>();
a1.add("java01");
a1.add("java02");
a1.add("java03");
a1.add("java04");
//a1.add(4); //編譯時就會報錯,如果不用泛型,即上面使用
//ArrayList a=new ArrayList()的方式,則此處在編譯的時候不會報錯,
//通過泛型,在編譯時報錯,從而達到了一種安全性檢查
Iterator<String> it=a1.iterator();
while(it.hasNext())
{
String s=it.next();
System.out.println(s+" : "+s.length());
}
}
public static void generic_customerize()
{
Tool t=new Tool();
t.setObject(new Worker());
t.getObject();
t.setObject(new Teacher());
t.getObject();
Utils<Worker> w=new Utils<Worker>();
w.setObject(new Worker());
w.getObject();
//用於展示泛型方法
MethodGenericDemo<String> m=new MethodGenericDemo<String>();
m.show("hello world");
m.print("hello world");
MethodGenericDemo<Integer> m1=new MethodGenericDemo<Integer>();
m1.show(100);
m1.print(200);
// 同一個對象,同一個方法,可以打印不同的類型,泛型在方法層面的應用
MethodGenericDemo2 m2=new MethodGenericDemo2();
m2.show("hello java");
m2.show(1000);
m2.print("hello world");
m2.print(2000);
//同時定義了泛型類和泛型方法的,即可以用泛型類固定方法中的引用數據類型
//也可以通過普通類調用不同的引用數據類型的泛型方法
//如下 m3實例調用的非泛型方法只能用string類型,而m33可以
MethodGenericDemo3<String> m3=new MethodGenericDemo3<String>();
m3.show("hello world");
// m3.show(new Integer(100)); show 方法未定義泛型,需要功能泛型類的類型保持一致
m3.print("hello world");
m3.print(100);
MethodGenericDemo3 m33=new MethodGenericDemo3();
m33.show("hello world");
m33.show(200);
m33.print("hello world");
m33.print(100);
//靜態方法泛型
MethodGenericDemo3.method("hello world");
MethodGenericDemo3.method(100);
//泛型定義在接口上使用實例
InterImpl i=new InterImpl();
i.show("hello world");
//泛型定義的接口,實現類繼續用泛型
InterImp2 i2=new InterImp2();
i2.show("hello world");
i2.show(100);
}
//類型不確定時可以使用問號?,表示佔位符
public static void printAll(ArrayList<?> a)
{
Iterator<?> it=a.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
//泛型限定
public static void printAllP(ArrayList<? extends PPerson> a)
{
Iterator<?> it=a.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void method_advance()
{
ArrayList<String> a1=new ArrayList<String>();
a1.add("abc01");
a1.add("abc02");
a1.add("abc03");
ArrayList<Integer> a2=new ArrayList<Integer>();
a2.add(100);
a2.add(200);
a2.add(300);
printAll(a1);
printAll(a2);
ArrayList<PPerson> p1=new ArrayList<PPerson>();
p1.add(new PPerson("abc"));
p1.add(new PPerson("abcd"));
p1.add(new PPerson("abcde"));
printAllP(p1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
genericArrayList();
genericTreeSet();
genericTreeSet2();
generic_customerize();
method_advance();
}
}
class LenComparator implements Comparator<String>
{
public int compare(String o1,String o2)
{
int num=new Integer(o1.length()).compareTo(new Integer(o2.length()));
if(num==0)
{
return o1.compareTo(o2);
}
return num;
}
}
class Worker
{
}
class Teacher
{
}
//泛型前的處理方法
class Tool
{
private Object obj;
public void setObject(Object obj)
{
this.obj=obj;
}
public Object getObject()
{
return this.obj;
}
}
// 泛型處理方法:定義泛型類
class Utils<T>
{
private T t;
public void setObject(T t)
{
this.t=t;
}
public T getObject()
{
return this.t;
}
}
// 用於演示定義泛型類的方法
class MethodGenericDemo<T>
{
public void show(T t)
{
System.out.println("show:"+t);
}
public void print(T t)
{
System.out.println("print:"+t);
}
}
//用於演示泛型方法
class MethodGenericDemo2
{
public <T> void show(T t)
{
System.out.println("show:"+t);
}
public <T> void print(T t)
{
System.out.println("print:"+t);
}
}
//同時泛型類和泛型方法
class MethodGenericDemo3<T>
{
public void show(T t)
{
System.out.println("show:"+t);
}
public <T> void print(T t)
{
System.out.println("print:"+t);
}
//靜態方法不能訪問類中的泛型
// public static void method(T t)
// {
// System.out.println("method: "+t);
// }
//如果靜態方法引用數據不確定,需要將泛型定義在方法上
public static <T> void method(T t)
{
System.out.println("method :"+t);
}
}
// 泛型定義在接口上
interface Inter<T>
{
void show(T t);
}
//實現接口的時候使用具體數據類型
class InterImpl implements Inter<String>
{
public void show(String t)
{
System.out.println("show: "+t);
}
}
//實現接口的時候仍然使用泛型
class InterImp2<T> implements Inter<T>
{
public void show(T t)
{
System.out.println("show :"+t);
}
}
class PPerson
{
private String name;
PPerson(String name)
{
this.name=name;
}
public String getName()
{
return this.name;
}
}
執行結果爲:
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