两个 Java 方法可以具有相同的名称但返回类型不同吗?

发布于 2024-10-31 03:00:45 字数 110 浏览 1 评论 0原文

两个 Java 方法可以具有相同名称返回类型不同吗?方法的返回类型不同,并且使用相同的方法名称进行声明。

这是允许的吗?

Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name.

Is that allowed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

追我者格杀勿论 2024-11-07 03:00:45

如果两个方法具有相同的参数类型,但返回类型不同,则这是不可能的。来自 Java 语言规范,Java SE 8 版本,§8.4 .2.方法签名

如果两个方法或构造函数 M 和 N 具有相同的名称、相同的类型参数(如果有)(第 8.4.4 节),并且在将 N 的形式参数类型调整为M的类型参数,与形参类型相同。

如果两种方法具有不同的参数类型(因此,它们具有不同的签名),那么这是可能的。这称为超载。

If both methods have same parameter types, but different return type than it is not possible. From Java Language Specification, Java SE 8 Edition, §8.4.2. Method Signature:

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.

If both methods has different parameter types (so, they have different signature), then it is possible. It is called overloading.

鸵鸟症 2024-11-07 03:00:45

仅当它们接受不同的参数时。如果没有参数,那么必须有不同的名称。

int doSomething(String s);
String doSomething(int); // this is fine


int doSomething(String s);
String doSomething(String s); // this is not

Only, if they accept different parameters. If there are no parameters, then you must have different names.

int doSomething(String s);
String doSomething(int); // this is fine


int doSomething(String s);
String doSomething(String s); // this is not
一笔一画续写前缘 2024-11-07 03:00:45

然而,根据 JLS,由于 Java 6/7 编译器(Oracle 的 JDK、OpenJDK、IBM 的 JDK)中的功能/错误,如果您使用泛型,您不能为相同的方法签名拥有不同的返回类型。

public class Main {
    public static void main(String... args) {
        Main.<Integer>print();
        Main.<Short>print();
        Main.<Byte>print();
        Main.<Void>print();
    }

    public static <T extends Integer> int print() {
        System.out.println("here - Integer");
        return 0;
    }
    public static <T extends Short> short print() {
        System.out.println("here - Short");
        return 0;
    }
    public static <T extends Byte> byte print() {
        System.out.println("here - Byte");
        return 0;
    }
    public static <T extends Void> void print() {
        System.out.println("here - Void");
    }
}

如需了解更多详细信息,请

here - Integer
here - Short
here - Byte
here - Void

在此处阅读我的文章

According the JLS, you cannot however due to a feature/bug in the Java 6/7 compiler (Oracle's JDK, OpenJDK, IBM's JDK) you can have different return types for the same method signature if you use generics.

public class Main {
    public static void main(String... args) {
        Main.<Integer>print();
        Main.<Short>print();
        Main.<Byte>print();
        Main.<Void>print();
    }

    public static <T extends Integer> int print() {
        System.out.println("here - Integer");
        return 0;
    }
    public static <T extends Short> short print() {
        System.out.println("here - Short");
        return 0;
    }
    public static <T extends Byte> byte print() {
        System.out.println("here - Byte");
        return 0;
    }
    public static <T extends Void> void print() {
        System.out.println("here - Void");
    }
}

Prints

here - Integer
here - Short
here - Byte
here - Void

For more details, read my article here

一曲爱恨情仇 2024-11-07 03:00:45

不可以。C++ 和 Java 都不允许重载函数的返回类型。原因是返回类型的重载可能会令人困惑(开发人员很难预测将调用哪个重载)。事实上,有人认为任何重载都可能在这方面造成混乱,并建议反对它,但即使是那些赞成重载的人似乎也同意这种特殊形式太令人困惑了。

No. C++ and Java both disallow overloading on a functions's return type. The reason is that overloading on return-type can be confusing (it can be hard for developers to predict which overload will be called). In fact, there are those who argue that any overloading can be confusing in this respect and recommend against it, but even those who favor overloading seem to agree that this particular form is too confusing.

筱武穆 2024-11-07 03:00:45

仅当其中一个方法是继承的并且返回类型兼容时,才可以使用两个具有相同参数和不同返回类型的方法。

例如:

public class A
{
    Object foo() { return null; }
}

public class B
    extends A
{
    String foo() { return null; }
}

You can have two methods with the same arguments and different return types only if one of the methods is inherited and the return types are compatible.

For example:

public class A
{
    Object foo() { return null; }
}

public class B
    extends A
{
    String foo() { return null; }
}
能怎样 2024-11-07 03:00:45

java 文档指出:

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

请参阅: http://docs.oracle.com/javase/tutorial/java /javaOO/methods.html

The java documentation states:

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.

See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

吃兔兔 2024-11-07 03:00:45

即使这是一个旧线程,也许有些人感兴趣。

如果您可以选择在同一个类中使用相同方法并归档不同的返回类型,请使用泛型:Oracle 课程泛型

泛型值持有者类的简单示例:

class GenericValue<T> {
  private T myValue;

  public GenericValue(T myValue) { this.myValue = myValue; }

  public T getVal() { return myValue; }
}

并像这样使用它:

public class ExampleGenericValue {
  public static void main(String[] args) {
    GenericValue<Integer> intVal = new GenericValue<Integer>(10);
    GenericValue<String> strVal = new GenericValue<String>("go on ...");

    System.out.format("I: %d\nS: %s\n", intVal.getVal(), strVal.getVal());
  }
}

... 将产生以下输出:

I: 10
S: go on ...

Even if it is an old thread, maybe some is interested.

If it is an option to you to use the same method inside the same class and archive different return types, use generics: Oracle Lesson Generics

Simple example for generic value holder class:

class GenericValue<T> {
  private T myValue;

  public GenericValue(T myValue) { this.myValue = myValue; }

  public T getVal() { return myValue; }
}

And use it like this:

public class ExampleGenericValue {
  public static void main(String[] args) {
    GenericValue<Integer> intVal = new GenericValue<Integer>(10);
    GenericValue<String> strVal = new GenericValue<String>("go on ...");

    System.out.format("I: %d\nS: %s\n", intVal.getVal(), strVal.getVal());
  }
}

... will result in the following output:

I: 10
S: go on ...
み零 2024-11-07 03:00:45

仅当它们的参数声明与内存不同时。

Only if their parameter declarations are different from memory.

那请放手 2024-11-07 03:00:45

如果它在同一个类中,具有相同数量的参数、相同的类型和顺序,
那么这是不可能的,例如:

int methoda(String a,int b) {
        return b;
}
String methoda(String b,int c) {
        return b;    
}

如果参数的数量及其类型相同但顺序不同,那么这是可能的,因为它会导致方法重载。这意味着方法签名是否相同,其中包括方法名称、参数数量及其类型以及定义的顺序。

If it's in the same class with the equal number of parameters with the same types and order,
then it is not possible for example:

int methoda(String a,int b) {
        return b;
}
String methoda(String b,int c) {
        return b;    
}

if the number of parameters and their types is same but order is different then it is possible since it results in method overloading. It means if the method signature is same which includes method name with number of parameters and their types and the order they are defined.

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