【Java学习笔记】Java6泛型实例-阿里云开发者社区

开发者社区> 开发与运维> 正文

【Java学习笔记】Java6泛型实例

简介:

你若是不使用泛型,则会这样处理数据类型不确定的问题:

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/archive/2012/12/17/2822245.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章