Java基礎 泛型

泛型

使用尖括號 <類型> 說明集合存儲的數據類型
書寫在返回值前邊,定義後邊。

import java.util.*;
/* 
泛型:JDK1.5 版本以後出現新特性。用於解決安全問題,是一個安全機制。

好處:
1.將運行時期出現問題ClassCastException,轉移到了編譯時期
方便程序員解決問題。讓運行時問題減少,安全

2.避免了強制轉換麻煩。

 */
class GenericDemo
{
    public static void main(String[] args)
    {
        ArrayList<String> al = new ArrayList<String>();
        al.add("abc");
        al.add("abc1");
        al.add("abc2");

        Iterator<String> it = al.iterator();
        while(it.hasNext())
        {
            String s=it.next();
            System.out.println(s);
        }
    }
}

泛型使用

import java.util.*;
class GenericDemo2
{
    public static void main(String[] args)
    {
        LenComparator lc=new LenComparator(); 
        TreeSet<String> ts=new TreeSet<String>(lc);

        ts.add("abcdefg");
        ts.add("abcde");
        ts.add("abcg");
        ts.add("abefg");
        ts.add("cdefg");
        ts.add("efg");

        Iterator<String> it=ts.iterator();

        while(it.hasNext())
        {
            String s=it.next();
            System.out.println(s);
        }

    }

}

class LenComparator implements Comparator<String>
{
    public int compare(String o1,String o2)
    {
        if(o1.length()>o2.length())
            return 1;
        if(o1.length()<o2.length())
            return -1;
        return o1.compareTo(o2);


    }
}

泛型類

import java.util.*;
/* class Tool
{
    private Worker w;
    public void setWorker(Worker w)
    {
        this.w=w;
    }
    public Worker getWorker()
    {
        return w;
    }
} */
class Worker
{

}

class Student
{

}
//泛型前做法
class Tool
{
    private Object obj;
    public void setObject(Object obj)
    {
        this.obj=obj;
    }
    public Object getObject()
    {
        return obj;
    }
}
//泛型類
/*
什麼時候定義泛型類?
當類中要操作的引用數據類型不確定的時候
早期定義 Object 來完成擴展
現在定義泛型來完成擴展
*/
class Utils<T>
{
    private T t;
    public void setObject(T t)
    {
        this.t=t;
    }
    public T getObject()
    {
        return t;
    }
}

class GenericDemo3
{
    public static void main(String[] args)
    {
        Utils<Worker> ut = new Utils<Worker>();
        ut.setObject(new Worker());
        Worker w=ut.getObject();
        /* Tool t=new Tool();
        t.setObject(new Worker());
        Worker w=(Worker)t.getObject();

        Tool t2=new Tool();
        t2.setObject(new Student());
        Student stu=(Student)t.getObject(); */
    }
}

泛型方法

在類中函數void之後定義 泛型
該泛型 T只在當前函數內生效.不影響類中其他方法的泛型

import java.util.*;

/* class Demo<T>
{
    public void show(T t)
    {
        System.out.println("show:"+t);
    }
    public void print(T t)
    {
        System.out.println("print:"+t);
    }
} */

//泛型類定義的泛型在整個類中有效,如果被方法使用,
//那麼泛型類的對象明確要操作的具體類型後,所有要操作的類型就已經固定了
//爲了讓不同方法可以操作不同類型,而且類型還不確定
//那麼可以將泛型定義在方法上。

class Demo
{
    public <T> void show(T t)
    {
        System.out.println("show:"+t);
    }
    public <T> void print(T t)
    {
        System.out.println("print:"+t);
    }
}
class GenericDemo4
{
    public static void main(String[] args)
    {
        /* Demo<Integer> d1 = new Demo<Integer>();

        d1.show(567);
        d1.print(new Integer(555));

        Demo<String> d2 = new Demo<String>();

        d2.show("abc");
        d2.print(new String("haha")); */
        Demo d = new Demo();
        d.show("haha");
        d.show(new Integer(5));
        d.print("ok");
    }
}

泛型類和泛型方法同時定義

class Demo<T>
{
    public void show(T t) //show使用類的泛型定義T
    {
        System.out.println("show:"+t);
    }
    public <E> void print(E e) //print使用方法的局部泛型定義E,改成T也可以正常使用,和類定義的T是不同的泛型。因爲方法名定義的是局部泛型
    {
        System.out.println("print:"+e);
    }
}

在main方法中使用下面方式使用該類

Demo<String> d = new Demo<String>();//定義了T是String類型
d.show("haha");
//d.show(new Integer(5));//傳入非String類型編譯失敗
d.print("ok");//傳入不同類型不影響
d.print(5);//

靜態泛型方法

靜態泛型方法定義

/*
特殊之處:
靜態方法不可以訪問類上定義的泛型。
如果靜態方法操作的應用數據類型不確定,可以將泛型定義在方法上。
*/
class Demo<T>
{
    public void show(T t)
    {
        System.out.println("show:"+t);
    }
    public <T> void print(T e)
    {
        System.out.println("print:"+e);
    }
    public static <W> void method(W t)//靜態泛型方法
    {
        System.out.println("method:"+t);
    }
}

調用方法:

Demo.method("static");

泛型接口

泛型定義在接口上

import java.util.*;

interface Inter<T>
{
    void show(T t);
}

/* class InterImpl implements Inter<String>
{
    public void show(String t)
    {
        System.out.println("Show:"+t);
    }
} */

class InterImpl<T> implements Inter<T>
{
    public void show(T t)
    {
        System.out.println("Show:"+t);
    }
}

class GenericDemo5
{
    public static void main(String[] args)
    {
        /* InterImpl i= new InterImpl();
        i.show("ok"); */

        InterImpl<Integer> i = new InterImpl<Integer>();
        i.show(4);
    }
}

輸出:
Show:4

泛型限定 ? 佔位符

import java.util.*;
class GenericDemo6
{
    public static void main(String[] args)
    {
        ArrayList<String> al = new ArrayList<String>();

        al.add("abc1");
        al.add("abc2");
        al.add("abc3");

        ArrayList<Integer> al1 = new ArrayList<Integer>();
        al1.add(4);
        al1.add(5);
        al1.add(6);

        printColl2(al);
        printColl2(al1);
    }

    public static  void printColl(ArrayList<?> al)//使用?代表佔位符,不明確具體類型,
    {
        Iterator<?> it = al.iterator();
        while(it.hasNext())
        {
            System.out.println(it.next());
        }
    }

    public static <T> void printColl2(ArrayList<T> al)//使用T可以在函數內對該類型進行操作
    {
        Iterator<T> it = al.iterator();
        while(it.hasNext())
        {
            T t=it.next();
            System.out.println(t);
        }
    }
}

泛型限定1 < ? extends 類型>

泛型的限定:
? extends E: 可以接受 E 類型或者E的子類型,上限
? super E:可以接受E類型或者E的父類型,下限


import java.util.*;
class GenericDemo6
{
    public static void main(String[] args)
    {
        ArrayList<Person> al2 = new ArrayList<Person>();
        ArrayList<Student> al3= new ArrayList<Student>();
        al2.add(new Person("lili"));
        al2.add(new Person("nana"));

        al3.add(new Student("kiki"));
        al3.add(new Student("qqqq"));
        printColl3(al2);
        printColl3(al3);
    }
    public static void printColl3(ArrayList<? extends Person> al)//使用? extends 限定類型
    {
        Iterator<? extends Person> it = al.iterator();
        while(it.hasNext())
        {
            //Person p=(Person)it.next();
            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);
    }
}

輸出:
lili
nana
kiki
qqqq

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章