Java中泛型的概念

来源:互联网 发布:为什么打开软件会闪退 编辑:程序博客网 时间:2024/04/29 09:20


/** * Created by lx on 2016/10/26. */class Point<T>{    private T id;    public T getId() {        return id;    }    public void setId(T id) {        this.id = id;    }}public class GenericTest {    public static void main(String[] args) {        Point<String> point=new Point<String>();        point.setId("123");        System.out.println(point.getId());        Point<Integer> point1=new Point<Integer>();        point1.setId(123);        System.out.println(point1.getId());    }}

泛型通过?实现通配符简直就是美妙极了

/** * Created by lx on 2016/10/26. */class Infor<T>{    private T key;    public T getKey() {        return key;    }    public void setKey(T key) {        this.key = key;    }    @Override    public String toString() {        return "Infor{" +                "key=" + key +                '}';    }}public class GenericTest {    public static void main(String[] args) {        Infor<String> infor=new Infor<String>();        infor.setKey("我的名字是群哥!");        print(infor);    }    public static void print(Infor<?> t){        System.out.println(t);    }}


0 0
原创粉丝点击