/*命名冲突
* 如果父类的方法名与接口的默认方法名冲突,则父类优先
* 如果多个接口中默认方法有相同名字,则子类必须实现重新实现*/
package testInertface;
/*测试接口的静态方法,默认方法*/
public class TestInterface2 {
public static void main(String[] args) {
testA ta=new testA();
ta.moren1();
ta.moren2();
A1.staticMethod();//通过接口名直接调用接口的静态方法
ta.staticMethod();//调用类的静态方法
testA.staticMethod();
}
}
interface B1{
default void moren1(){
System.out.println("B1的默认方法1");
}
}
interface A1{
default void moren1(){
System.out.println("A1的默认方法1");
}
default void moren2(){
System.out.println("A1的默认方法2");
}
static void staticMethod(){
System.out.println("A1的静态方法");
}
}
class testA implements A1{
static void staticMethod(){
System.out.println("testA的静态方法");
}
}
class testB implements A1,B1{//接口冲突,接口中有相同名字的静态方法moren1,需要重写
@Override
public void moren1() {
B1.super.moren1();
}
}
java接口2
最新推荐文章于 2024-10-27 15:49:02 发布