【发布时间】:2015-04-20 16:13:38
【问题描述】:
问题:
我有以下:
interface A
{
int MethodA(int x, int y);
}
和
interface B extends A
{
//This is meant to overload MethodA
int MethodA(int x, int y, int z);
}
当我尝试实现接口 B 并在 main 中运行该方法时,问题就来了。
class Foo implements B
{
//For interface B
public int MethodA(int x, int y, int z)
{
//Actual implementation
}
//For interface A
public int MethodA(int x, int y)
{
//Actual implementation
}
}
public static void main(String[] s)
{
Foo foo = new Foo();
//Problem occurs here
foo.MethodA(1, 2, 3);
}
Eclipse 会抱怨说“类型 A 中的方法 MethodA(int, int, int) 不适用于参数 (int, int)。”
但是为什么呢?我已经实现了接口 B。MethodA(int, int, int) 不应该重载 MethodA(int, int),让 MethodA 也能接受三个整数吗?
【问题讨论】:
-
在 Java 7 中为我工作,当然我已经纠正了给定代码中的所有编译错误(返回类型、公共修饰符、参数名称)
-
这不是 Java 代码。这是纯文本。
-
这里所谓的编译错误似乎不适用。也许该行打错了,应该是:A foo = new Foo();
标签: java overloading