java - 我可以用另一个采用相同参数但具有不同返回类型的方法重载一个方法吗?

标签 java operator-overloading

例如我可以有这样的东西吗:

public static void SAMENAME (sameparameter n) 
{
    some code ;
} 

public static String SAMENAME (sameparameter n) 
{
    similar code; 
    return someString ; 
} 

最佳答案

这是不允许的。

Java中的方法签名被认为是方法名和参数列表。返回类型不是方法签名的一部分。

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

来源:http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html

关于java - 我可以用另一个采用相同参数但具有不同返回类型的方法重载一个方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2526621/

相关文章:

java - 在abstractmap类内部,为什么remove()不显示UnsupportedOperationException?

c++ - 返回常量引用

java.net.ConnectException : failed to connect to/10. 0.0.2(端口 80):连接失败:ETIMEDOUT(连接超时)

java - Object[] 是如何克隆的

java - Gson json获取结果

java - 将 Intent 文件路径共享到文件管理器以打开该路径

c++ - 使用 << 运算符同时写入文件和 cout

fortran - 访问运算符 "[ ], ( ), { }"在 Fortran 90 或 2003 中重载

c++ - 多维数组中的 Operator[] 重载 C++

C++ << 运算符重载和 Arduino 模板