黑马程序员技术交流社区

标题: 泛型函数参数问题 [打印本页]

作者: yanghu    时间: 2014-6-13 21:14
标题: 泛型函数参数问题
  1. public class Test2 {
  2.         public static <T> void show(Set<T> a, Set<T> b){
  3.                 System.out.println(b.getClass().getName());
  4.         }
  5.         public static void main(String[] args) {
  6.                 Set<String> a = new HashSet<String>();
  7.                 Set<Integer> b = new HashSet<Integer>();
  8.                 /*String a = new String("asda");
  9.                 Integer b = new Integer(12); */
  10.                 Test2.show(a, b);
  11.         }
  12. }
复制代码


为什么调用Test2.show(a, b)编译会出错,求解答,谢谢。
作者: 波涛    时间: 2014-6-13 21:31
因为a,b的类型不一致啊!
作者: yinxjfly    时间: 2014-6-13 21:43


作者: yinxjfly    时间: 2014-6-13 21:50
public class GenericDemo {

        public static void main(String[] args) {
                 Set<String> a = new HashSet<String>();
                 Set<String> a1 = new HashSet<String>();
         Set<Integer> b = new HashSet<Integer>();
         /*String a = new String("asda");
         Integer b = new Integer(12); */
         show(a, b);

        }
        public static void show(Set<?> a, Set<?> b){
        System.out.println(a.getClass().getName());
        System.out.println(b.getClass().getName());
}
}       
我觉得这样写就了没什么问题了!





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2