接口

接口就是公共规范

在这里插入图片描述

接口的内容包括

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

接口的抽象方法与默认方法

接口
public interface MyinterfaceAbstract {
    //我们是抽象方法
    public abstract void methed1();
    abstract void methed2();
    public void methed3();
    void methed4();
    //我是默认方法
    public default void methed5() {
        System.out.println("这是新添加的默认方法");
    }
}
实现类一
public class MyinterfeceAbstractImpl implements MyinterfaceAbstract{
    @Override
    public void methed1() {
        System.out.println("我是实现类一的第一个方法");
    }
    @Override
    public void methed2() {
        System.out.println("我是实现类一的第二个方法");
    }
    @Override
    public void methed3() {
        System.out.println("我是实现类一的第三个方法");
    }
    @Override
    public void methed4() {
        System.out.println("我是实现类一的第四个方法");
    }
}
实现类二
public class MyinterfaceAbstratImpl2 implements MyinterfaceAbstract{
    @Override
    public void methed1() {
        System.out.println("我是实现类二的第一个方法");
    }
    @Override
    public void methed2() {
        System.out.println("我是实现类二的第二个方法");
    }
    @Override
    public void methed3() {
        System.out.println("我是实现类二的第三个方法");
    }
    @Override
    public void methed4() {
        System.out.println("我是实现类二的第四个方法");
    }

    @Override
    public void methed5() {
        System.out.println("我覆盖重写了接口的默认方法");
    }
}
测试
public class Interface {
    public static void main(String[] args) {
        MyinterfeceAbstractImpl impl = new MyinterfeceAbstractImpl();
        //我们执行的是抽象方法
        impl.methed1();
        impl.methed2();
        impl.methed3();
        impl.methed4();
        //我执行的是默认方法,我的类中没有该方法,这个方法是从接口中来的
        impl.methed5();
        MyinterfaceAbstratImpl2 impl2 = new MyinterfaceAbstratImpl2();
        impl2.methed1();
        impl2.methed2() ;
        impl2.methed3() ;
        impl2.methed4();
        //我执行的是默认方法,但是我覆盖重写了接口的默认方法
        impl2.methed5();
    }
}

在这里插入图片描述
在这里插入图片描述

静态方法

接口
public interface MyinterfaceStatic {
    public static void methoed(){
        System.out.println("这是一个静态方法");
    }
}
实现类
public class MyinterfaceStaticImpl implements MyinterfaceStatic{
}
测试
public class ineterface {
    public static void main(String[] args) {
        MyinterfaceStaticImpl impl = new MyinterfaceStaticImpl();
        // 错误写法
//        impl.methed();
        //正确写法,而且不用实例化对象
        MyinterfaceStatic.methoed();
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

静态方法默认方法代码只差在default与static

接口默认方法
public interface MyinterfacePrivate {
    public default void methed1(){
        System.out.println("默认方法一执行了");
        methed3();
    }
    public default void methed2(){
        System.out.println("默认方法二执行了");
        methed3();
    }
    private void methed3(){
        System.out.println("AAA");
        System.out.println("BBB");
        System.out.println("CCC");
    }
}
实现类中无法调用privae
public class MyinterfacePrivateImpl implements MyinterfacePrivate{
}

接口常量用法

在这里插入图片描述
在这里插入图片描述

接口内容小结,重中之重

在这里插入图片描述

接口的特点及注意事项

在这里插入图片描述
在这里插入图片描述

同名方法,优先父类的方法,不会执行接口的方法

接口A
public interface MyinerfaceA {
    //抽象方法
    public abstract void methed1();
    public abstract void methedAbs();
    //默认方法
    public default void metheddef(){
        System.out.println("默认方法A");
    }
}
接口B
public interface MyinerfaceB {
    public abstract void methed2();
    public abstract void methedAbs();
    public default void metheddef(){
        System.out.println("默认方法B");
    }
}
实现类
public class interdace implements MyinerfaceA,MyinerfaceB{
    // 覆盖重写所有的抽象方法,重复名称的重写一次即可
    @Override
    public void methed1() {
        System.out.println("覆盖重写了A方法");
    }
    @Override
    public void methedAbs() {
        System.out.println("覆盖重写了AB接口都有的方法");
    }
    @Override
    public void methed2() {
        System.out.println("覆盖重写了B方法");
    }
    //共有的默认方法,必须重写
    @Override
    public void metheddef(){
        System.out.println("重写AB共有的默认方法默认方法");
    }
}

接口的多继承

在这里插入图片描述
在这里插入图片描述

接口1
public interface Myinerface1 {
    //抽象方法
    public abstract void methed1();
    public abstract void methedAbs();
    //默认方法
    public default void metheddef(){
        System.out.println("AAA");
    }
}
接口2
public interface Myinerface2 {
    //抽象方法
    public abstract void methed2();
    public abstract void methedAbs();
    public default void metheddef(){
        System.out.println("BBB");
    }
}
子接口
public interface Myinterface extends Myinerface1,Myinerface2{
    public abstract void methed();

    @Override
    default void metheddef() {
        System.out.println("我要带着default覆盖重写1,2接口的默认方法");
    }
}
实现类
public class Interface implements Myinterface{
    @Override
    public void methed() {

    }
    @Override
    public void methed1() {

    }

    @Override
    public void methed2() {

    }

    @Override
    public void methedAbs() {

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值