1. 一个接口继承多个接口,如果多个接口中存在方法签名冲突(被继承的接口中方法名称相同),则此时不支持多继承
interface I() {
void test1();
}
interface J() {
String test1();
}
interface K extends I,J {
//无法继承I,J两个接口,因为这两个接口中的方法名一样了
}
2. 一个类实现多个接口,如果多个接口中存在方法签名冲突,则此时不支持多实现
interface I() {
void test1();
}
interface J() {
String test1();
}
class K implements I,J {
//不支持多实现,因为I,J接口中方法签名相同了
}
3. 一个类继承了父类,同时实现了多接口,如果父类和接口中有同名的默认方法,实现类会优先用父类中的
class Father() {
public void run() {
//父类的run方法
}
}
interface IT() {
default void run() {
//接口中的run方法
}
}
class Sun extends Father implements IT {
}
public class Main() {
public static void main(String[] args) {
Sun sun = new Sun();
sun.run(); //此处会优先调用Father类中的run方法
}
}
4. 一个类实现了多个接口,多个接口中存在同名的默认方法,可以不冲突,重写该方法即可
interface I {
default void text() {
//I接口中的默认方法test
System.out.println("I接口");
}
}
interface J {
default void test() {
//J接口中的默认方法test
System.out.println("J接口");
}
}
class A implements I,J {
@Override
public void test() {
System.out.println("自己重写一套test方法,不调用接口中的默认方法");
}
}