在 Java 中使用不同的返回类型重载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2439782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:40:10  来源:igfitidea点击:

Overload with different return type in Java?

javaoverloading

提问by nunos

Why is it not possible to overload a function just by changing the return type? Will that change in a future version of Java?

为什么不能仅通过更改返回类型来重载函数?这会在 Java 的未来版本中改变吗?

By the way, just for reference, is this possible in C++?

顺便说一句,仅供参考,这在C++中可行吗?

采纳答案by Alexander Gessler

You can't do it in Java, and you can't do it in C++. The rationale is that the return value alone is not sufficient for the compiler to figure out which function to call:

你不能用 Java 来做,你也不能用 C++ 来做。理由是仅返回值不足以让编译器确定要调用哪个函数:

public int foo() {...}
public float foo() {..}

...
foo(); // which one?

回答by Oded

The reason is that overloads in Java are only allowed for methods with different signatures.

原因是 Java 中的重载只允许用于具有不同签名的方法。

The return type is not part of the method signature, hence cannot be used to distinguish overloads.

返回类型不是方法签名的一部分,因此不能用于区分重载。

See Defining Methodsfrom the Java tutorials.

请参阅Java 教程中的定义方法

回答by Daneel S. Yaitskov

Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.

在 Java 5.0 之前,当您覆盖一个方法时,参数和返回类型必须完全匹配。在 Java 5.0 中,它引入了一种称为协变返回类型的新工具。您可以覆盖具有相同签名的方法,但返回所返回对象的子类。换句话说,子类中的方法可以返回一个对象,该对象的类型是超类中具有相同签名的方法返回的类型的子类。

回答by Ganesh

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

编译器在区分方法时不考虑返回类型,因此您不能声明具有相同签名的两个方法,即使它们具有不同的返回类型。

回答by Vinayak

Return type does not matter while overloading a method. We just need to ensure there is no ambiguity!

重载方法时返回类型无关紧要。我们只需要确保没有歧义!

The only way Java can know which method to call is by differentiating the types of the argument list. If the compiler allowed two methods with the same name and same argument types, there would be no way to determine which one it should call.

Java 知道调用哪个方法的唯一方法是区分参数列表的类型。如果编译器允许两个具有相同名称和相同参数类型的方法,则无法确定它应该调用哪一个。

回答by Abdullah Khan

Overloadedmethods in java may have different return types given that the argument is also different.

Overloaded鉴于参数也不同,java 中的方法可能具有不同的返回类型。

Check out the sample code.

查看示例代码。

public class B {

    public String greet() {
        return "Hello";
    }

    //This will work
    public StringBuilder greet(String name) {
        return new StringBuilder("Hello " + name);
    }

    //This will not work
    //Error: Duplicate method greet() in type B
    public StringBuilder greet() {
        return new StringBuilder("Hello Tarzan");
    }

}

回答by MAYANK AMRIT

no not really possible that way you can only overload by no of arguments or data type of the arguments

不是真的不可能那样你只能通过没有参数或参数的数据类型来重载

回答by Bhanu

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

编译器在区分方法时不考虑返回类型,因此您不能声明具有相同签名的两个方法,即使它们具有不同的返回类型。

If you are aware of function execution then you will be aware that when we call a function the definition part executes and at last we require the return statement, hence we can say return comes after the function's whole defintion, thats why if there are two or more functions with same name and with same type and no. of arguments then at the time of calling how compiler will know about which one to be called, because function name and parameters are the same. At the time of calling firstly all the focus will be on arguments and function name and after the completion of function definition at last we deal with return statement.

如果你知道函数执行,那么你就会知道当我们调用一个函数时,定义部分会执行,最后我们需要 return 语句,因此我们可以说 return 在函数的整个定义之后出现,这就是为什么如果有两个或更多具有相同名称、相同类型和编号的函数。参数然后在调用时编译器将如何知道要调用哪个,因为函数名称和参数是相同的。在首先调用的时候,所有的焦点都会放在参数和函数名上,在函数定义完成后,我们最后处理 return 语句。

Compile Time Error is better than Run Time Error. So, java compiler renders compiler time error if you declare the same method having same parameters.

编译时错误优于运行时错误。因此,如果您声明具有相同参数的相同方法,java 编译器会呈现编译器时间错误。