如何对空参数进行方法重载?
我添加了三个带参数的方法:
public static void doSomething(Object obj) {
System.out.println("Object called");
}
public static void doSomething(char[] obj) {
System.out.println("Array called");
}
public static void doSomething(Integer obj) {
System.out.println("Integer called");
}
当我调用 doSomething(null)
时,编译器会抛出错误作为不明确的方法。那么问题是因为 Integer
和 char[]
方法还是 Integer
和 Object
方法?
I have added three methods with parameters:
public static void doSomething(Object obj) {
System.out.println("Object called");
}
public static void doSomething(char[] obj) {
System.out.println("Array called");
}
public static void doSomething(Integer obj) {
System.out.println("Integer called");
}
When I am calling doSomething(null)
, then compiler throws error as ambiguous methods. So is the issue because Integer
and char[]
methods or Integer
and Object
methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 将始终尝试使用可用方法的最具体适用版本(请参阅 JLS §15.12.2)。
Object
、char[]
和Integer
都可以将null
作为有效值。因此,所有 3 个版本都适用,因此 Java 必须找到最具体的一个。由于
Object
是char[]
的超类型,因此数组版本比Object
版本更具体。因此,如果仅存在这两种方法,则将选择 char[] 版本。当
char[]
和Integer
版本均可用时,两者都比Object
更具体,但是没有一个比另一个更具体,因此 Java 无法决定调用哪一个。在这种情况下,您必须通过将参数转换为适当的类型来明确提及要调用哪一个。请注意,实际上这一问题的发生率比人们想象的要少得多。原因是,只有当您使用
null
或使用相当不特定类型的变量(例如Object
)显式调用方法时,才会发生这种情况。相反,下面的调用是完全明确的:
虽然您仍然传递值
null
,但 Java 确切地知道要调用哪个方法,因为它将考虑变量的类型。Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).
Object
,char[]
andInteger
can all takenull
as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.Since
Object
is the super-type ofchar[]
, the array version is more specific than theObject
-version. So if only those two methods exist, thechar[]
version will be chosen.When both the
char[]
andInteger
versions are available, then both of them are more specific thanObject
but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with
null
or with a variable of a rather un-specific type (such asObject
).On the contrary, the following invocation would be perfectly unambiguous:
Although you're still passing the value
null
, Java knows exactly which method to call, since it will take the type of the variable into account.当使用
null
参数调用时,这三个方法中的每一对本身都是不明确的。因为每个参数类型都是引用类型。以下是使用 null 调用您的一种特定方法的三种方法。
如果您确实打算使用
null
参数调用这些方法,我可以建议您消除这种歧义。这样的设计会在未来招致错误。Each pair of these three methods is ambiguous by itself when called with a
null
argument. Because each parameter type is a reference type.The following are the three ways to call one specific method of yours with null.
May I suggest to remove this ambiguity if you actually plan to call these methods with
null
arguments. Such a design invites errors in the future.null
是这三种类型中任何一种的有效值;所以编译器无法决定使用哪个函数。请改用doSomething((Object)null)
或doSomething((Integer)null)
之类的内容。null
is a valid value for any of the three types; so the compiler cannot decide which function to use. Use something likedoSomething((Object)null)
ordoSomething((Integer)null)
instead.Java中的每个类都扩展了Object类。甚至Integer类也扩展了Object。因此,Object 和 Integer 都被视为 Object 实例。因此,当您将 null 作为参数传递时,编译器会混淆要调用哪个对象方法,即使用参数 Object 或参数 Integer,因为它们都是对象,并且它们的引用可以为 null。但java中的原语并不扩展Object。
Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.
我已经尝试过这一点,当恰好存在一对重载方法并且其中一个具有参数类型 Object 时,编译器将始终选择具有更具体类型的方法。但是,当存在多个特定类型时,编译器会抛出不明确的方法错误。
由于这是一个编译时事件,因此只有当有人故意将 null 传递给该方法时才会发生这种情况。如果这是故意这样做的,那么最好在不带参数的情况下再次重载此方法,或者完全创建另一个方法。
I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then the compiler throws an ambiguous method error.
Since this is a compile time event, this can only happen when one intentionally passes null to this method. If this is done intentionally then it is better to overload this method again with no parameter or create another method altogether.
输出是称为 null 的 Int,因此 char[] 和 Integer 存在歧义
The output is Int called null and so ambiguity is with char[] and Integer
由于
doSomething(char[] obj)
和doSomething(Integer obj)
存在歧义。char[] 和 Integer 对于 null 来说都是相同的上级,这就是它们不明确的原因。
there is an ambiguity because of
doSomething(char[] obj)
anddoSomething(Integer obj)
.char[] and Integer both are the same superior for null that's why they are ambiguous.