在 Java 中哪个重载会被选择为 null? [英] Which overload will get selected for null in Java?

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

问题描述

如果我用 Java 写这一行:

If I write this line in Java:

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

会调用哪个方法?

  • 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.

推荐答案

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

The most specific method will be called - in this case

showInputDialog(Component parent, Object message)

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

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.

这篇关于在 Java 中哪个重载会被选择为 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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