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

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

问题描述

如果我用Java写这行:

If I write this line in Java:

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

将调用哪种方法?


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

  • showInputDialog(对象消息,对象initialSelectionValue)

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


如果多个成员方法都是
可访问且适用于方法
调用,则需要选择
one来提供e
运行时方法调度的描述符。 Java
编程语言使用规则
最具体的方法是
选择。

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天全站免登陆