问题描述
一个接口可以在Java中扩展多个接口吗?此代码在我的 IDE 中显示有效,并且可以编译:
Can an interface extend multiple interfaces in Java? This code appears valid in my IDE and it does compile:
interface Foo extends Runnable, Set, Comparator<String> { }
但我听说Java中不允许多重继承.为什么会出现接口异常?
but I had heard that multiple inheritance was not allowed in Java. Why does there appear to be an exception for interfaces?
推荐答案
是的,你可以做到.一个接口可以扩展多个接口,如下所示:
Yes, you can do it. An interface can extend multiple interfaces, as shown here:
interface Maininterface extends inter1, inter2, inter3 {
// methods
}
一个类也可以实现多个接口.如果两个接口有一个定义相同名称和签名的方法怎么办?
A single class can also implement multiple interfaces. What if two interfaces have a method defining the same name and signature?
有一个棘手的问题:
interface A {
void test();
}
interface B {
void test();
}
class C implements A, B {
@Override
public void test() {
}
}
然后单个实现适用于两者:)
Then single implementation works for both :).
在此处阅读我的完整帖子:
Read my complete post here:
http://codeinventions.blogspot.com/2014/07/can-interface-extend-multiple.html
这篇关于一个接口可以在 Java 中扩展多个接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!