J a v a Java Java 中泛型的使用

1.定义一个含有泛型的类

package 泛型;

public class genericClass<E>{
    private E name;

    public E getName() {
        return name;
    }

    public void setName(E name) {
        this.name = name;
    }
}

2.主类

package 泛型;

public class MaInClass {
    public static void main(String[] args) {
        genericClass<String> a1 = new genericClass<String>();
        a1.setName("HeHao");
        System.out.println(a1.getName());
        genericClass<Integer> a2= new genericClass<Integer>();
        a2.setName(1234);
        System.out.println(a2.getName());
    }
}