在 Java 的重载方法中使用 null [英] Using null in overloaded methods in Java

查看:23
本文介绍了在 Java 的重载方法中使用 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
NULL 参数的方法重载

以下代码编译并运行良好.

The following code compiles and goes fine.

public class Main
{
    public void temp(Object o)
    {
        System.out.println("The method with the receiving parameter of type Object has been invoked.");
    }

    public void temp(String s)
    {
        System.out.println("The method with the receiving parameter of type String has been invoked.");
    }

    public void temp(int i)
    {
        System.out.println("The method with the receiving parameter of type int has been invoked.");
    }

    public static void main(String[] args)
    {
        Main main=new Main();
        main.temp(null);
    }
}

在这段代码中,要调用的方法是接受类型为String

In this code, the method to be invoked is the one that accepts the parameter of type String

文档 说.

如果多个成员方法既可访问又适用于一个方法调用,需要选择一个来提供运行时方法分派的描述符.Java编程语言使用的规则是选择最具体的方法.

If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

但是我不明白代码中接受原始int参数的方法之一什么时候被修改为接受包装类型Integer的参数比如,

but I don't understand when one of the methods in the code that accepts the parameter of the primitive int is modified to accept the parameter of the wrapper type Integer such as,

public void temp(Integer i)
{
    System.out.println("The method with the receiving parameter of type Integer has been invoked.");
}

发出编译时错误.

对 temp 的引用不明确,两个方法 temp(java.lang.String) 都在methodoverloadingpkg.Main 和方法 temp(java.lang.Integer) inmethodoverloadingpkg.Main 匹配

reference to temp is ambiguous, both method temp(java.lang.String) in methodoverloadingpkg.Main and method temp(java.lang.Integer) in methodoverloadingpkg.Main match

在这种特殊情况下,为什么使用原始数据类型重载方法是合法的,而其对应的包装器类型似乎不是这种情况?

In this particular scenario, why is it legal to overload a method with a primitive data type which however doesn't appear to be the case with its corresponding wrapper type?

推荐答案

如果有人问你字符串"或对象"哪个更专业,你会说什么?显然是字符串",对吧?

If you were asked what is more specialized "String" or "Object", what would you say? Evidently "String", right?

如果有人问您:字符串"或整数"哪个更专业?没有答案,它们都是对象的正交特化,您如何在它们之间进行选择?那么你必须明确说明你想要哪个.例如通过转换您的空引用:

If you were asked: what is more specialized "String" or "Integer"? There is no answer, they are both orthogonal specializations of an object, how can you choose between them? Then you must be explicit regarding which one you want. For instance by casting your null reference:

question.method((String)null)

当你使用原始类型时,你不会有这个问题,因为null"是一个引用类型,不能与原始类型冲突.但是当您使用引用类型时,null"可以指代 String 或 Integer(因为 null 可以转换为任何 reference 类型).

When you use primitive types you do not have that problem because "null" is a reference type and cannot conflict with primitive types. But when you use reference types "null" could refer to either String or Integer (since null can be cast to any reference type).

请参阅我在上述评论中发布的另一个问题中的答案,了解更多更深入的细节,甚至一些引用自 JLS.

See the answer in the other question that I posted in the comments above for further and deeper details and even a few quotes from the JLS.

这篇关于在 Java 的重载方法中使用 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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