Java doesn't allow the multiple inheritance to protect diamond problem. It uses interface to take care of this problem.
然后说使用接口(interface)的情况
interface A{
run();
}
interface B{
run();
}
class C implements A, B{
run() {} //Which interface we are using?
}
当我们调用类C
中的方法run()
时,我们如何判断我们使用的是哪个接口(interface)?
最佳答案
你不知道。这并不重要,因为实现不在接口(interface)上,而是在类上。所以实现是独一无二的。没有歧义。
重要的是是否每个声明都希望有不同的返回类型:
interface A{
void run();
}
interface B{
String run();
}
class C implements A, B{
???? run() {}
}
这是您在 Java 中遇到多个接口(interface)问题的唯一方法。
关于java - 从两个不同的接口(interface)调用相同的方法名称 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19959371/