如果我用 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/