Java中方法的签名是否包含其返回类型? [英] Does a method's signature in Java include its return type?

查看:168
本文介绍了Java中方法的签名是否包含其返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java类/接口中的方法签名是否包含其返回类型?

Does the method signature in a Java class/interface include its return type?

示例:

Java是否知道这两种方法之间的区别:

Does Java know the difference between those two methods:

public class Foo {
    public int  myMethod(int param) {}
    public char myMethod(int param) {}
}

或者是它可能只是重要的方法名称和参数列表?

Or is it maybe only the method name and parameters list that matters?

推荐答案

引自 Oracle Docs


定义:方法声明的两个组成部分包括 方法签名 - 方法的名称和参数类型。

Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

由于编辑了问题以包含此示例:

Since the question was edited to include this example:

public class Foo {
    public int  myMethod(int param) {}
    public char myMethod(int param) {}
}

不,编译器不会知道差异,因为他们的签名: myMethod(int param)是一样的。第二行:

No, the compiler won't know the difference, as their signature: myMethod(int param) is the same. The second line:

    public char myMethod(int param) {}

会给出错误:方法已经在课程中定义,这进一步证实了上述声明。

will give you can error: method is already defined in class, which further confirms the above statement.

这篇关于Java中方法的签名是否包含其返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆