【Java學習筆記】Java6泛型執行個體

來源:互聯網
上載者:User

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

你若是不使用泛型,則會這樣處理資料類型不確定的問題:

class Stash {
    private Object x;
    void set(Object x) {
        this.x = x;
    }
    Object get() {
        return(x);
    }
}
public class StashOne {
    public static void main(String arg[]) {
        Stash stash = new Stash();
        stash.set("abcd");
        String str = (String)stash.get();
    }
}

使用了泛型則:

class StashString {
    private String x;
    void set(String x) {
        this.x = x;
    }
    String get() {
        return(x);
    }
}
public class StashTwo {
    public static void main(String arg[]) {
        StashString stash = new StashString();
        stash.set("abcd");
        String str = stash.get();
    }
}

你也可以在建立對象的時候規定類型:

class Stash<T> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashThree {
    public static void main(String arg[]) {
        Stash<String> stash = new Stash<String>();
        stash.set("abcd");
        String str = stash.get();
    }
}

或者在賦值的時候規定:

class Stash<T> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashFour {
    public static void main(String arg[]) {
        Stash<Object> stash = new Stash<Object>();
        stash.set("abcd");
        String str = (String)stash.get();
    }
}

要想規定某個參數是某各類及其子類的泛型,則:

class Stash<T extends Number> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashFive {
    public static void main(String arg[]) {
        Stash<Integer> istash = new Stash<Integer>();
        Integer ten = new Integer(10);
        istash.set(ten);
        ten = istash.get();
        Stash<Double> dstash = new Stash<Double>();
        Double pi = new Double(3.14159);
        dstash.set(pi);
        pi = dstash.get();
    }
}

對於介面:

import java.util.EventListener;
import javax.swing.JTable;
import javax.swing.undo.UndoManager;
class Stash<T extends EventListener> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashSix {
    public static void main(String arg[]) {
        Stash<JTable> tablestash = new Stash<JTable>();
        JTable table = new JTable();
        tablestash.set(table);
        table = tablestash.get();
        Stash<UndoManager> dstash = new Stash<UndoManager>();
        UndoManager unman = new UndoManager();
        dstash.set(unman);
        unman = dstash.get();
    }
}

而你要是想既規定類又規定實現了某一個介面,那麼:

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Transparency;
class Stash<T extends Image & Transparency> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashSeven {
    public static void main(String arg[]) {
        Stash<BufferedImage> bufstash = new Stash<BufferedImage>();
        BufferedImage bufimage = new BufferedImage(50,50,0);
        bufstash.set(bufimage);
        bufimage = bufstash.get();
    }
}

而萬用字元的泛型類可以使你在建立泛型類的指標時可以模糊處理:

class Stash<T> {
    private T x;
    void set(T x) {
        this.x = x;
    }
    T get() {
        return(x);
    }
}
public class StashEight {
    public static void main(String arg[]) {
        Stash<? extends Number> numberstash;
        Stash<Integer> integerstash;
        integerstash = new Stash<Integer>();
        integerstash.set(new Integer(10));
        numberstash = integerstash;
        Number number = numberstash.get();
        System.out.println(number.toString());
        Stash<Double> doublestash;
        doublestash = new Stash<Double>();
        doublestash.set(new Double(3.14159));
        numberstash = doublestash;
        Double dnumber = (Double)numberstash.get();
        System.out.println(dnumber.toString());
    }
}

泛型還可以嵌套:

class Pair<T,U> {
    private T left;
    private U right;
    Pair(T l, U r) {
        left = l;
        right = r;
    }
    public T getLeft() {
        return(left);
    }
    public U getRight() {
        return(right);
    }
}
class Stash<T> {
    private T t;
    void set(T t) {
        this.t = t;
    }
    T get() {
        return(t);
    }
}
public class Nesting {
    @SuppressWarnings("unchecked")
    public static void main(String arg[]) {
        Stash<Pair<String,Long>> sp;
        sp = new Stash<Pair<String,Long>>();
        Pair pair = new Pair("Average",new Long(320));
        sp.set(pair);
        pair = sp.get();
        System.out.println(pair.getLeft() + " " + pair.getRight());
    }
}

另外泛型不只是類,方法也可以泛型:

import java.awt.Color;
public class GenericMethod {
    public static void main(String arg[]) {
        GenericMethod gm = new GenericMethod();
        gm.objtype("abcd");
        gm.gentype("abcd");
        gm.objtype(Color.green);
        gm.gentype(Color.green);
    }
    public void objtype(Object t) {
        System.out.println(t.getClass().getName());
    }
    public <T> void gentype(T t) {
        System.out.println(t.getClass().getName());
    }
}

 

作者:gnuhpc
出處:http://www.cnblogs.com/gnuhpc/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.