所以我有一个类“MyClass”和两个接口(interface)“A”和“B”
public class MyClass implements A, B {
@Override
public void mymethod(){
System.out.println("hello world");
}
}
...
public interface A {
void mymethod();
}
...
public interface B {
void mymethod();
}
这里什么接口(interface)的什么方法被覆盖了?
我在想两者都是?
谢谢 (学生)
最佳答案
在这种情况下,就好像只有一种方法。在这种情况下,它将覆盖“两种”方法,因为它们具有完全相同的签名。如果这些方法有不同的签名(返回类型或其他),那么就会有问题。查看此答案 here
例如:
public class MyClass implements A, B {
@Override
public void mymethod(){
System.out.println("hello world");
}
}
...
public interface A {
void mymethod();
}
...
public interface B {
String mymethod();
}
这会导致问题...
关于java - 具有相同方法名称的两个接口(interface),当我覆盖时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150658/