泛型的使用
作者:互联网
public class Test1 { public static void main(String[] args) { Test2<String> test2 = new Test2<String>(); Test2<Integer> test21 = new Test2<Integer>("name",12,34); test2.setEve("yeye"); System.out.println(test2); System.out.println(test21); System.out.println("============================================"); DTest test = new DTest();//DTest类是一个普通类,不是泛型类 test.setEve(6); System.out.println(test); System.out.println("============================================="); DTest1<Object> objectDTest1 = new DTest1<>();//DTest1类是一个是泛型类 objectDTest1.setEve("ya"); System.out.println(objectDTest1); objectDTest1.setEve(12); System.out.println(objectDTest1); } } //自定义的泛型类 class Test2<T> { String name; int age; T eve; public T getEve() { return eve; } public void setEve(T eve) { this.eve = eve; } public Test2() { //如果要造一个T类型的数组,不可以: // T[] arr = new T[10]; T[] arr = (T[]) new Object[10]; } public Test2(String name, int age, T eve) { this.name = name; this.age = age; this.eve = eve; } @Override public String toString() { return "name='" + name + '\'' + ", age=" + age + ", eve=" + eve ; } } //泛型类的子类 class DTest extends Test2<Integer>{}; class DTest1<T> extends Test2<T>{};
标签:Test2,name,System,eve,使用,泛型,public,out 来源: https://www.cnblogs.com/hxisme/p/15767783.html