class A
{
void f(Integer i)
{
System.out.println("hello");
}
void f(float f)
{
System.out.println("world");
}
}
class  Test14
{
public static void main(String[] args) 
{
A a=new A();
int i=8;
a.f(i);
}
}结果是调用了 f(float f)方法 为什么啊
关于函数的重载的时候 java对于编译器根据参数,选择一个方法,如果没有完全匹配的,怎么解决

解决方案 »

  1.   

    void f(Integer i) 

    System.out.println("hello"); 

    参数类型是Integer的,而你传入f方法的是int类型的,匹配了float参数的f方法
      

  2.   

    class A {
    void f(Integer i) {
    System.out.println("hello");
    } void f(byte f) {
    System.out.println("world");
    }
    }public class Test14 {
    public static void main(String[] args) {
    A a = new A();
    int i = 8;
    a.f(i);
    }
    }楼主问的问题很有趣哦
    基础类型找不到兼容的,就找它的封装类去了
    楼主多试验试验,很有意思
      

  3.   

    格式格式,忘掉了 =。=
    对不起
    class A {
    void f(Integer i) {
    System.out.println("hello");
    } void f(byte f) {
    System.out.println("world");
    }
    }public class Test14 {
    public static void main(String[] args) {
    A a = new A();
    int i = 8;
    a.f(i);
    }
    }
      

  4.   

    Integer 是个对象,而不是简单的变量,所以不能和一般的变量混为一谈,而float只是对象,而且能容纳int,就用它了