泛型主要还是起约束作用。简单给个示例,希望对你有帮助 :public class Test1 { @Test void test() { Sample<String, String, Boolean, Boolean, Long, Long> sample = new Sample(); sample.a = "string"; sample.b = "string"; sample.t = true; sample.u = false; sample.ab = 123L; sample.cde = 345L; Sample<Boolean, Boolean, Long, Long, String, String> sample1 = new Sample(); sample1.a = true; sample1.b = false; sample1.t = 123L; sample1.u = 345L; sample1.ab = "string"; sample1.cde = "string"; } } /** * 泛型主要用于进行约束,实际使用中常用T,U,V等,实际上可以使用任意值. */ class Sample<A, b, T, u, AB, cdE> { //a的类型声明为A,对应Sample上的A // 所以在使用时字段a的类型必须与声明sample时的第一个泛型类型相同 public A a; public b b; public T t; public u u; public AB ab; public cdE cde; }
泛型主要还是起约束作用。简单给个示例,希望对你有帮助 :