泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,即所操作的数据类型被指定为一个参数。
类名<类型名>
/** * 汽车类 */ @Data public class Car { /*无参构造*/ public Car() { } /*带参构造*/ public Car(int id, String name, String Type) { this.id = id; this.name = name; this.Type = Type; } /*属性*/ private int id;//编号 private String name;//名称 private String Type;//类型 }
/** * 宠物类 */ @Data public class Pet { /*无参构造*/ public Pet() { } /*带参构造*/ public Pet(int id, String name, String Type) { this.id = id; this.name = name; this.Type = Type; } /*属性*/ private int id;//编号 private String name;//名称 private String Type;//类型 }
/** * 泛型测试类 * * @param <T> 参数1(泛型对象) * @param <S> 参数2(继承Pet类的泛型对象) */ @Data public class User<T, S extends Pet> { /*带参构造*/ public User(int id, String name, String Type, List<T> t, List<S> s) { this.id = id; this.name = name; this.Type = Type; this.tList = t; this.sList = s; } /*泛型属性*/ private List<T> tList;//泛型List private List<S> sList;//继承Pet类的泛型List /*一般属性*/ private int id;//编号 private String name;//名称 private String Type;//类型 /*重写toString()方法*/ @Override public String toString() { String msg = "id为【%s】的【%s】用户【%s】拥有猫猫【%s】和汽车【%s】"; return String.format(msg, this.id, this.Type, this.name, this.sList.toString(), this.tList.toString()); } }
/** * 测试类 */ public class Test { public static void main(String args[]) { List<Pet> petList = new ArrayList<Pet>(); petList.add(new Pet(1, "汤圆", "英短")); petList.add(new Pet(2, "糯米", "英短")); petList.add(new Pet(3, "咖喱", "田园")); List<Car> carList = new ArrayList<Car>(); carList.add(new Car(1, "标志", "小轿车")); carList.add(new Car(2, "哈弗", "SUV")); User user = new User(1, "ZH", "男性", carList, petList);//第5个参数如果不是一个继承了Pet类的List,则编译会报错 System.out.println(user.toString()); } }
我们知道Object类是所有类的父类,在引入泛型概念之前,我们常用Object的引用来实现参数的“任意化”。
痛点:
泛型的好处:
原文:https://www.cnblogs.com/riches/p/12158874.html