Java 中哪个重载会被选择为 null?

发布于 2024-08-06 19:05:40 字数 287 浏览 2 评论 0原文

如果我用 Java 写这行:

JOptionPane.showInputDialog(null, "Write something");

哪个方法会被调用?

  • showInputDialog(组件父级,对象消息)
  • showInputDialog(对象消息,对象initialSelectionValue)

我可以测试它。但在其他类似的情况下,我想知道会发生什么。

If I write this line in Java:

JOptionPane.showInputDialog(null, "Write something");

Which method will be called?

  • showInputDialog(Component parent, Object message)
  • showInputDialog(Object message, Object initialSelectionValue)

I can test it. But in other cases similar to this, I want to know what happens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

◇流星雨 2024-08-13 19:05:40

将调用最具体的方法 - 在这种情况下,

showInputDialog(Component parent, Object message)

这通常位于"确定方法签名" 规范 (15.12.2) 中重载解析的步骤,特别是 “选择最具体的方法”

在不深入细节的情况下(您可以在规范中阅读这些细节),简介给出了很好的总结:

如果多个成员方法同时是
可访问并适用于方法
调用时,需要选择
一个提供描述符
运行时方法调度。爪哇
编程语言使用规则
最具体的方法是
选择。

非正式的直觉是
方法比另一种方法更具体
如果第一个处理了任何调用
方法可以传递给其他人
没有编译时类型错误。

The most specific method will be called - in this case

showInputDialog(Component parent, Object message)

This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".

Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:

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.

The informal intuition is that one
method is more specific than another
if any invocation handled by the first
method could be passed on to the other
one without a compile-time type error.

孤蝉 2024-08-13 19:05:40

在您的特定情况下,将调用更具体的方法。但一般来说,在某些情况下方法签名可能不明确。请考虑以下情况:

public class Main {

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

    private void testNullArgument( Object o )
    {
        System.out.println("An Object was passed...");
    }

    private void testNullArgument( Integer i )
    {
        System.out.println("An Integer was passed...");
    }

    private void testNullArgument( String s )
    {
        System.out.println("A String was passed...");
    }
}

在这种情况下,编译器无法在采用 Integer 的方法和采用 String 的方法之间做出决定。当我尝试编译它时,我得到

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

In your particular case the more specific method will be called. In general, though, there are some cases where the method signature can be ambiguous. Consider the following:

public class Main {

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

    private void testNullArgument( Object o )
    {
        System.out.println("An Object was passed...");
    }

    private void testNullArgument( Integer i )
    {
        System.out.println("An Integer was passed...");
    }

    private void testNullArgument( String s )
    {
        System.out.println("A String was passed...");
    }
}

In this case, the compiler can't decide between the method that takes an Integer and the method that takes a String. When I try to compile that, I get

reference to testNullArgument is ambiguous, both method testNullArgument(java.lang.Integer) in testnullargument.Main and method testNullArgument(java.lang.String) in testnullargument.Main match
叹沉浮 2024-08-13 19:05:40

<罢工>都不是。您将收到编译器错误,要求您澄清要调用什么方法。您可以通过显式转换第一个参数来做到这一点:

showInputDialog((Object) null, "Write something");

showInputDialog((Component) null, "Write something");

更新 我应该知道 - 永远不要怀疑 Jon Skeet。我上面提到的问题仅在无法确定哪种方法更具体时才会出现。这是一个测试用例:

public class Test {

  public void doSomething(String arg1, Object arg2) {
    System.out.println("String, Object");
  }

  public void doSomething(Object arg1, String arg2) {
    System.out.println("Object, String");
  }

  public static void main(String[] args) {
    Test test = new Test();
    test.doSomething(null, null);
  }
}

上面将给出编译器错误。

Neither. You'll get a compiler error asking you to clarify what method you want to call. You can do so by explicitly casting the first argument:

showInputDialog((Object) null, "Write something");

or

showInputDialog((Component) null, "Write something");

Update I should have known - never doubt Jon Skeet. The problem I've referred to above only occurs when it's impossible to determine which method is more specific. Here's a test case:

public class Test {

  public void doSomething(String arg1, Object arg2) {
    System.out.println("String, Object");
  }

  public void doSomething(Object arg1, String arg2) {
    System.out.println("Object, String");
  }

  public static void main(String[] args) {
    Test test = new Test();
    test.doSomething(null, null);
  }
}

The above will give a compiler error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文