泛型
舉例子:由於java中,編譯和運行是分開的,有時候我門在集合中存儲數據時,沒有定義存儲數據類型是什麼,這時在編譯過程中是看不見的,但是在運行過程中才會報異常。所以我們需要將異常在編譯時就體現出異常。這時候就產生泛型。我們在創建集合對象時,通過泛型定義集合要存儲的數據對象類型。這樣,如果我們在編寫代碼時,傳入泛型定義意外的對象類型,那麼就會在編譯過程中報出異常!
JDK1.5版本後出現的新特性,用於解決安全問題,是一個類型安全機制。
好處
1,類將異常轉移到了編譯時期。
將運行時期出現的ClassCastException,轉移到了編譯時期。
放便與程序員解決問題,讓運行事情問題減少,安全。
2,避免了強制轉換的麻煩。
缺點
不能使用一些類的特有方法。
泛型格式
通過<>來定義要操作的引用數據類型。
什麼時候寫泛型呢?
通常在集合框架中很常見,只要見到<>就要定義泛型。
其實<>就是用來接收類型的,
當使用集合時,將集合中要存儲的數據類型作爲參數傳遞到<>中即可。
注意
compareTo 可以使用泛型。
但是equals切記不能使用泛型
使用泛型類,在ArrayList中打印出集合元素的長度
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("abc01");
al.add("abc0991");
al.add("abc013");
al.add("abc014");
Iterator<String> it = al.iterator();
while (it.hasNext())
{
String s= it.next();
sop(s+"="+s.length());
}
}
public static void sop (Object obj)
{
System.out.println(obj);
}
}
通過泛型類,將TreeSet中的元素按長度排序import java.util.*;
class Test
{
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String>(new Length());
ts.add("abcde");
ts.add("aabd");
ts.add("cc");
ts.add("dea");
Iterator<String> it =ts.iterator();
while (it.hasNext())
{
String s = it.next();
sop(s);
}
}
public static void sop (Object obj)
{
System.out.println(obj);
}
}
class Length 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;
}
}
泛型類(泛型定義在類上)
什麼時候定義泛型類
當類中要操作的引用數據類型不確定的時候
現在定義泛型來定義擴展
import java.util.*;
class Utils<QQ>
{
private QQ q;
public void setObject(QQ q)
{
this.q = q;
}
public QQ getObject()
{
return q;
}
}
class Worker
{
}
class Test
{
public static void main(String[] args)
{
Utils<Worker> u = new Utils<Worker>();
u.setObject(new Worker());
Worker w = u.getObject();
}
public static void sop (Object obj)
{
System.out.println(obj);
}
}
泛型 定義在方法上
泛型類定義的泛型,在整個類中有效
如果被方法使用,那麼泛型類的對象明確要操作的具體類型後,所有要操作的類型就已經固定了。
爲了讓不同方法可以操作不同類型,而且類型還不確定
那麼可以將泛型定義在法方上。
泛型方法寫在返回值前面 <T > void
import java.util.*;
class Demo
{
public <T> void show(T t)
{
System.out.println("show :"+t);
}
public <W> void print(W w)
{
System.out.println("print:" +w);
}
}
class Test
{
public static void main(String[] args)
{
Demo d = new Demo();
d.show("abzc");
d.print("wokao");
d.print(3);
}
public static void sop (Object obj)
{
System.out.println(obj);
}
}
特殊之處----泛型與靜態
因爲靜態方法隨着類的產生而加載
泛型類隨着對象的創建才能確定
靜態方法不可以訪問定義在類上的泛型。
如果靜態方法操作的應用數據不確定,可以將泛型定義在方法上。
import java.util.*;
class Demo <T>
{
public void show(T t)
{
System.out.println("show :"+t);
}
public <W> void print(W w)
{
System.out.println("print:" +w);
}
public static <Z> void sop (Z z)
{
System.out.println("sop:"+z);
}
}
class Test
{
public static void main(String[] args)
{
Demo<String> d = new Demo<String>();
d.show("abzc");
d.print("wokao");
d.print(3);
d.sop("gsdf");
}
public static void sop (Object obj)
{
System.out.println(obj);
}
}
泛型定義在接口上
interface Demo <T>
{
void show (T t);
}
class DemoImpl implements Demo<String>
{
public void show(String s)
{
System.out.println(s);
}
}
class Test
{
public static void main(String[] args)
{
DemoImpl d = new DemoImpl();
d.show("hello world");
}
}
泛型定義在接口上interface Demo <T>
{
void show (T t);
}
class DemoImpl<T> implements Demo<String>
{
public void show(String s)
{
System.out.println(s);
}
}
class Test
{
public static void main(String[] args)
{
DemoImpl<Integer> d = new DemoImpl<Integer>();
d.show(36);
}
}
佔位符
<?>通配符,也可以理解爲佔位符
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList<String> al= new ArrayList<String>();
al.add("abc1");
al.add("abc2");
al.add("abc3");
ArrayList<Integer> al2=new ArrayList<Integer>();
al2.add(123);
al2.add(33);
al2.add(131);
printit(al);
printit(al2);
}
public static void printit(ArrayList<?> al)
{
Iterator<?> it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
泛型限定
? extends E :可以接受E類型或者E的子類型,上限限定
? super E : 可以接收E類型或者E的服類型,下限限定
下線限定
import java.util.*;
class Test
{
public static void main(String[] args)
{
ArrayList<Person> al= new ArrayList<Person>();
al.add(new Person("abc1"));
al.add(new Person("abc2"));
al.add(new Person("abc3"));
ArrayList<Student> al2=new ArrayList<Student>();
al.add(new Student("abc--1"));
al.add(new Student("abc--2"));
al.add(new Student("abc--3"));
printit(al);
printit(al2);
}
public static void printit(ArrayList<? extends Person> al)
{
Iterator<? extends Person> it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next().getName());
}
}
}
class Person
{
private String name;
Person(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
class Student extends Person
{
Student (String name)
{
super (name);
}
}
import java.util.*;
class Test
{
public static void main(String[] args)
{
TreeSet<Student> ts = new TreeSet<Student>(new comp());
ts.add(new Student("abc03"));
ts.add(new Student("abc02"));
ts.add(new Student("abc01"));
ts.add(new Student("abc04"));
Iterator<Student> it = ts.iterator();
while (it.hasNext())
{
System.out.println(it.next().getName());
}
TreeSet<Worker> ts1 = new TreeSet<Worker>(new comp());
ts1.add(new Worker("wabc03"));
ts1.add(new Worker("wabc02"));
ts1.add(new Worker("wabc01"));
ts1.add(new Worker("wabc04"));
Iterator<Worker> it1 = ts1.iterator();
while (it1.hasNext())
{
System.out.println(it1.next().getName());
}
}
}
class comp implements Comparator <Person>
{
public int compare(Person p1,Person p2)
{
return p1.getName().compareTo(p1.getName());
}
}
class Person
{
private String name;
Person(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String toString()
{
return "person:"+name;
}
}
class Student extends Person
{
Student (String name)
{
super (name);
}
}
class Worker extends Person
{
Worker (String name)
{
super (name);
}
}