null在此代码中如何工作? [英] How does null work in this code?

查看:61
本文介绍了null在此代码中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

null在此代码中如何工作,为什么不打印对象?

How does null work in this code, why doesn't it print object?

class Test1{
    public void doStuff(Object o){
    System.out.println("In Object");
}

    public void doStuff(String o){
        System.out.println("In String");
    }
}

public class TTest {
    public static void  main(String args[]){    
        Test1 t = new Test1();
        t.doStuff(null);
    }
}

输出:

在字符串中

推荐答案

Java将始终尝试使用最特定版本的方法.

Java will always try to use the most specific version of a method.

通话以来

t.doStuff(null);  

适用于两种方法

t.doStuff(Object o)
t.doStuff(String o)

Java将选择最具体的方法描述,即

Java will choose the most specific method description, which is

t.doStuff(String o)

这篇关于null在此代码中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆