mixpplus
9/16/2019 - 6:46 AM

泛型的使用

泛型的使用

package com.ingin.stream.fanxing;

public class People<T> {
    private  T key;

    public T getKey() {
        return key;
    }

    public void setKey(T key) {
        this.key = key;
    }

    public static void main(String[] args) {
//        People<java.io.Serializable> people = new People<java.io.Serializable>();
        People<Object> people = new People<>();
        people.get(10);
        people.get("changhao");
        people.get(true);
        people.getObj("常浩");
        people.getObj(true);
        System.out.println("-----------------------");
        people.getE(true);
        people.getE("hello");
        System.out.println("-----------------");
        people.getNum(10.1);
        people.getNum(10);
        System.out.println("------------------");
        people.setKey(10);
        Object num = people.getNum(20);
        Integer a = (Integer)num;
        System.out.println(a);

    }
    public void get(T t){
        System.out.println(t);
    }

    public <T> void getObj(T t){
        System.out.println(t);
    }

    public <E> void getE(E e){
        System.out.println(e);
    }

    public <E extends Number> T getNum(E t){
        System.out.println(t);

        return this.getKey();
    }
}