如何对空参数进行方法重载?

发布于 2024-10-20 13:41:16 字数 499 浏览 7 评论 0原文

我添加了三个带参数的方法:

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) 时,编译器会抛出错误作为不明确的方法。那么问题是因为 Integerchar[] 方法还是 IntegerObject 方法?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

度的依靠╰つ 2024-10-27 13:41:16

Java 将始终尝试使用可用方法的最具体适用版本(请参阅 JLS §15.12.2)。

Objectchar[]Integer 都可以将 null 作为有效值。因此,所有 3 个版本都适用,因此 Java 必须找到最具体的一个。

由于 Objectchar[] 的超类型,因此数组版本比 Object 版本更具体。因此,如果仅存在这两种方法,则将选择 char[] 版本。

char[]Integer 版本均可用时,两者都比 Object 更具体,但是没有一个比另一个更具体,因此 Java 无法决定调用哪一个。在这种情况下,您必须通过将参数转换为适当的类型来明确提及要调用哪一个。

请注意,实际上这一问题的发生率比人们想象的要少得多。原因是,只有当您使用 null 或使用相当不特定类型的变量(例如 Object)显式调用方法时,才会发生这种情况。

相反,下面的调用是完全明确的:

char[] x = null;
doSomething(x);

虽然您仍然传递值 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[] and Integer can all take null 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 of char[], the array version is more specific than the Object-version. So if only those two methods exist, the char[] version will be chosen.

When both the char[] and Integer versions are available, then both of them are more specific than Object 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 as Object).

On the contrary, the following invocation would be perfectly unambiguous:

char[] x = null;
doSomething(x);

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.

时间你老了 2024-10-27 13:41:16

当使用 null 参数调用时,这三个方法中的每一对本身都是不明确的。因为每个参数类型都是引用类型。

以下是使用 null 调用您的一种特定方法的三种方法。

doSomething( (Object) null);
doSomething( (Integer) null);
doSomething( (char[]) 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.

doSomething( (Object) null);
doSomething( (Integer) null);
doSomething( (char[]) 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.

埋情葬爱 2024-10-27 13:41:16

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 like doSomething((Object)null) or doSomething((Integer)null) instead.

哭泣的笑容 2024-10-27 13:41:16

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.

浅笑轻吟梦一曲 2024-10-27 13:41:16

我已经尝试过这一点,当恰好存在一对重载方法并且其中一个具有参数类型 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.

小忆控 2024-10-27 13:41:16
class Sample{
  public static void main (String[] args) {
       Sample s = new Sample();
       s.printVal(null);

    } 
    public static void printVal(Object i){
        System.out.println("obj called "+i);
    }

    public static void printVal(Integer i){
        System.out.println("Int called "+i);
    }
}

输出是称为 null 的 Int,因此 char[] 和 Integer 存在歧义

class Sample{
  public static void main (String[] args) {
       Sample s = new Sample();
       s.printVal(null);

    } 
    public static void printVal(Object i){
        System.out.println("obj called "+i);
    }

    public static void printVal(Integer i){
        System.out.println("Int called "+i);
    }
}

The output is Int called null and so ambiguity is with char[] and Integer

药祭#氼 2024-10-27 13:41:16

由于 doSomething(char[] obj)doSomething(Integer obj) 存在歧义。

char[] 和 Integer 对于 null 来说都是相同的上级,这就是它们不明确的原因。

there is an ambiguity because of doSomething(char[] obj) and doSomething(Integer obj).

char[] and Integer both are the same superior for null that's why they are ambiguous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文