gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:29:04 26 4
gpt4 key购买 nike

如果我用 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 bothaccessible and applicable to a methodinvocation, it is necessary to chooseone to provide the descriptor for therun-time method dispatch. The Javaprogramming language uses the rulethat the most specific method ischosen.

The informal intuition is that onemethod is more specific than anotherif any invocation handled by the firstmethod could be passed on to the otherone without a compile-time type error.

关于java - 在 Java 中,哪个重载会被选为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1545501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com