本帖最后由 潘星 于 2012-8-10 11:23 编辑  
 
class Generic  
{ 
        public static void main(String[] args)  
        { 
                Demo<String> d=new Demo<String>(); 
                d.show(12);//我在show方法中已经声明了参数类型为String,为什么传整数也可以? 
                d.print("12");//我在print方法中声明的为Integer,则可以传入正数而不能传入字符串 
        } 
} 
class Demo<Integer> 
{ 
        public <String> void show(String t) 
        { 
                System.out.println("show"+t); 
        } 
        public <Integer> void print(int t) 
        { 
                System.out.println("print"+t); 
        } 
} 
为什么定义了泛型为String的show方法确可以传入int类型数据,而定义泛型为 
Integer正数类型的方法print确不能传字符串? 
还有就是Demo类中的泛型是<Integer>,而创建Demo<String> d=new Demo<String>(); 
对象的时候确可以传入<String>,是什么原因呢? 
 |