1 class Info2 { 3 private T var; 4 5 public T getVar() { 6 return var; 7 } 8 9 public void setVar(T var) {10 this.var = var;11 }12 13 @Override14 public String toString() {15 return "Info [var=" + var + "]";16 }17 18 }19 20 static void fun(Info in)21 {22 System.out.println(in.hashCode());23 }24 public static void main(String[] args)25 {26 Info info=new Info ();27 info.setVar("wfasf");28 fun(info);29 }30
Infoi1=new Info<>(); Info i2=new Info<>(); Info i3=new Info<>(); i1.setVar(20); i2.setVar("sfdsdf"); combine(i1,i3); private static void combine(Info i1, Info i12) { System.out.println(i1.getVar()+" "+i12.getVar()); }
传递给combine的参数必须范型类型是相同的,否则编译出错。
Integer[] b=fun1(1,2,4,5,3);private staticT[] fun1(T... b) { return b; }
T... 可变数组