java - 在 Java 中,哪个重载会被选为 null?

标签 java parameters methods invocation

如果我用 Java 写这行代码:

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

会调用哪个方法?

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

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

最佳答案

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

showInputDialog(Component parent, Object message)

这通常属于 "Determine Method Signature"规范 (15.12.2) 中的重载解决步骤,特别是 "Choosing the Most Specific Method" .

没有深入细节(您可以在此处阅读规范中的内容),引言给出了很好的总结:

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 - 在 Java 中,哪个重载会被选为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1545501/

相关文章:

java - 为什么当我输入 3 个相同的数字时,我的输出总是相同?

java - Testng - 使用 setTestClasses() 传递参数

c# - 在 C# 中,当您在空对象上调用扩展方法时会发生什么?

ruby - 如何动态覆盖方法

java - 'public static void' 在 Java 中是什么意思?

Java正则表达式处理

java - 尝试传递二维数组时出现不兼容类型错误

java - 将传感器数据保存到android中的文件

c - 函数中的参数太少

java - 在方法之间传递变量的问题