class GenericExample<T extends Number,U>
{
T t;
U u;
public GenericExample(T t,U u)
{
this.t=t;
this.u=u;
}
public GenericExample()
{

} public void inspect() /*1*/
{
System.out.println("fun1 T:"+t.getClass().getName()+"\t"+"U:"+u.getClass().getName());
}
public void inspect(T t)/*2*/
{ System.out.println("fun2 T:"+t.getClass().getName());
}

public <U>void inspect(U w)/*4*/
{
System.out.println("fun4 W:"+w.getClass().getName());
}
public static void main(String arg[])
{
                GenericExample      GE2=new GenericExample();
GenericExample<?,?> GE3=new GenericExample<Integer,Float>();                GE2.inspect(new Integer(3));//调用函数2
GE3.inspect(new Integer(3));//调用函数4 }
}希望高手能解释一下,为什么GE2.inspect(Integer)调用的是2号函数
而GE3.inspect(Integer)调用的是四号函数?

解决方案 »

  1.   

    GenericExample GE2 = new GenericExample();
    GE2.inspect(new Integer(3)); //调用函数2
    会提示一个raw type的warning,在runtime会去调用 public void inspect(T t);sun也不推荐此用法GenericExample<?,?> GE3=new GenericExample<Integer,Float>();
    GE3.inspect(new Integer(3));//调用函数4 
    编译器不会报错,但由于给GenericExample设置了一个Unknown的类型.编译器由于不知道具体的类型,public void inspect(T t)是不会被调用的.
    执行时只会调用public <U>void inspect(U w).LZ可以把函数public <U>void inspect(U w)删掉,程序就无法编译了.
    个人浅见,欢迎有不同的答案.