java接口2

/*命名冲突
* 如果父类的方法名与接口的默认方法名冲突,则父类优先
* 如果多个接口中默认方法有相同名字,则子类必须实现重新实现*/

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值