V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
a1310747
V2EX  ›  程序员

问下这段代码最后会输出什么,为什么..

  •  
  •   a1310747 · 2017-03-04 11:24:46 +08:00 · 2747 次点击
    这是一个创建于 2609 天前的主题,其中的信息可能已经有所发展或是发生改变。
    public class Test {
    public void test(Object o) {
    System.out.println("Object");
    }
    public void test(String s) {
    System.out.println("String");
    }
    public static void main(String[] args) {
    Test that = new Test();
    that.test(null);
    }
    }
    15 条回复    2017-03-04 13:44:30 +08:00
    littleshy
        1
    littleshy  
       2017-03-04 11:32:02 +08:00
    string 吧,不同类型以具体的为准。
    ml3661190
        2
    ml3661190  
       2017-03-04 11:35:03 +08:00
    输出 String 具体为什么我也不清楚,坐等大神解答
    Kylinsun
        3
    Kylinsun  
       2017-03-04 11:35:46 +08:00 via Android
    你传的是什么类型调用的就是什么样的方法。
    chnyang
        4
    chnyang  
       2017-03-04 11:46:01 +08:00
    输出的不应该是 String 么 ..
    System.out.println("String");
    ml3661190
        5
    ml3661190  
       2017-03-04 11:47:20 +08:00
    不同类型以具体的为准。理解这句话应该就没问题了,我刚刚测试了一下

    当传入的参数,未能有具体类型重载方法时,选择入参的的超类,也就是 Object 入参的方法

    当有 Object , String , boolean 三个重载的方法时,传入 null 值,编译器则会报出一个 ambiguous 的编译异常

    因为 String 跟 boolean 的方法都属于具体的类型,编译器无法判断选择那一个

    大概就是这么个意思吧
    chnyang
        6
    chnyang  
       2017-03-04 11:47:20 +08:00
    @ml3661190 我是菜鸟..不知道这是不是坑,但是 test 方法唯一的一行代码只是输出一个固定的字符串而已.. 求轻喷
    yidinghe
        7
    yidinghe  
       2017-03-04 11:48:47 +08:00 via Android
    编译失败。
    chnyang
        8
    chnyang  
       2017-03-04 11:50:45 +08:00
    @ml3661190 然后发现两个 test 方法....我错了
    a1310747
        9
    a1310747  
    OP
       2017-03-04 11:52:06 +08:00
    @ml3661190 当有 Object , String , boolean 三个重载的方法时,传入 null 值,并不会报错 依然还是输出 String
    Cloudee
        10
    Cloudee  
       2017-03-04 11:52:41 +08:00 via iPhone
    compile error
    feiyuanqiu
        11
    feiyuanqiu  
       2017-03-04 12:12:37 +08:00
    jls8 §15.12.2
    ---------------
    There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is the one used at run time to perform the method dispatch.

    https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2
    depress
        12
    depress  
       2017-03-04 12:30:21 +08:00
    方法重载优先选子类
    ovear
        13
    ovear  
       2017-03-04 12:51:21 +08:00
    优先匹配子类呀。。
    feeeeeef
        14
    feeeeeef  
       2017-03-04 13:21:01 +08:00
    编译不通过
    ml3661190
        15
    ml3661190  
       2017-03-04 13:44:30 +08:00
    @a1310747 这...不知道如何截图,但是我的机器上 Eclipse 确实会报错

    具体信息如下

    The method test(Object) is ambiguous for the type Test
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1532 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:11 · PVG 01:11 · LAX 10:11 · JFK 13:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.