为什么输出是这样的?
class Another {
public void method(Object o) {
System.out.println("This is in method which takes object");
}
public void method(String s) {
System.out.println("This is method which takes string");
}
}
public class NewClass {
public static void main(String args[]) {
Another an = new Another();
an.method(null);
}
}
当我尝试执行此操作时,我得到
这是采用字符串的方法
这是将字符串作为输出的 。为什么不“这是在接受对象的方法中”?对象也可以为空,字符串也可以为空,为什么它不调用第一个方法?
class Another {
public void method(Object o) {
System.out.println("This is in method which takes object");
}
public void method(String s) {
System.out.println("This is method which takes string");
}
}
public class NewClass {
public static void main(String args[]) {
Another an = new Another();
an.method(null);
}
}
When I try to execute this, I get
This is method which takes string
as the output. Why not "This is in method which takes object"? Object can also be null and string can also be null, why doesn't it invoke first method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 Java 语言规范,在重载方法的情况下,两个方法都可以处理提供的参数,则选择具有更具体参数的方法。由于
String
比Object
更具体(String
extendsObject
),void method(String)< /code> 被选择并调用。
According to the Java Language Specification, in such cases of overloaded methods where both methods can handle the provided arguments, the method with more specific argument is chosen. Since
String
is more specific thanObject
(String
extendsObject
),void method(String)
is chosen and called.这与 JLS 中的指定完全相同:
String
是一个Object
,但并非所有Object
都是String
。因此,String
比Object
更具体,这也是为什么在上面的示例中选择String
重载的原因。值得注意的是,这个确切的问题(本质上)出现在精彩的 Java Puzzlers 中(强烈推荐),特别是谜题 46:令人困惑的构造函数的案例
我将引用Effective Java 2nd Edition,第 41 条:明智地使用重载来结束:
不用说,这本书也强烈推荐。
另请参阅
关于显式转换
简单:将
null
转换为Object
类型。在多种情况下这是有用/必要的,这就是其中之一。那么方法调用不明确,并且发生编译时错误。
要解决歧义,您可以再次将
null
(或其他表达式)转换为所需的重载类型。另请参见
This is exactly as specified in JLS:
A
String
is-aObject
, but not allObject
is-aString
. Therefore,String
is more specific thanObject
, hence why theString
overload is chosen in the above example.It is worth noting that this exact question (in essence) appeared in the wonderful Java Puzzlers (highly recommended), specifically Puzzle 46: The Case of the Confusing Constructor
I will close with a quote from Effective Java 2nd Edition, Item 41: Use overloading judiciously:
Needless to say, this book is also highly recommended.
See also
On explicit casting
Simple: cast the
null
to typeObject
. There are several scenarios where this is useful/necessary, and this is one of them.Then the method invocation is ambiguous, and compile-time error occurs.
To resolve the ambiguity, again you can cast the
null
(or other expression) to the desired overload type.See also
在解析重载方法调用时,编译器将始终找到可能的最窄匹配。
顺便说一句,Java 约定是对类使用大写名称,即
Another
。The compiler will always find the narrowest match possible when resolving overloaded method calls.
Btw the Java convention is to use uppercase names for classes, i.e.
Another
.我相当确定,在这种情况下,使用 Java 6(我认为 Java 5 会引发编译器错误),它将选择最低级别的类来将 null 归因于。 (由于 String 是一个对象,因此它在层次结构中处于较低位置)
I'm fairly certain that with Java 6 (I think Java 5 would have thrown a compiler error) in a case like this, it will select tho lowest level class to attribute the null to. (since String is an Object it is lower in the hierarchy)