Java接口学习笔记

接口组成更新

在这里插入图片描述

默认方法

在这里插入图片描述

public interface MyInterface {
    void show1();
    void show2();
    default void show3(){
        System.out.println("show3");
    }
}
class MyInterfaceImplOne implements MyInterface{
    @Override
    public void show1() {
        System.out.println("One show1");
    }

    @Override
    public void show2() {
        System.out.println("One show2");
    }
}
class MyInterfaceImplTwo implements MyInterface,MyInterfaceSon{
    @Override
    public void show1() {
        System.out.println("Two show1");
    }

    @Override
    public void show2() {
        System.out.println("Two show2");
    }

    @Override
    public void show3() {
        System.out.println("Two show3");
    }
}

public class MyInterfaceDemo {
    public static void main(String[] args) {
        MyInterface my = new MyInterfaceImplOne();
        my.show1();
        my.show2();
        my.show3();
        MyInterface my2 = new MyInterfaceImplTwo();
        my2.show1();
        my2.show2();
        my2.show3();
    }
}

在这里插入图片描述

静态方法

在这里插入图片描述

public interface Flyable {
    public static void test() {
        System.out.println("Flyable中的静态方法执行了");
    }
}
public interface Inter {
    void show();
    default void method(){
        System.out.println("Inter中的默认方法");
    }
    public static void test(){
        System.out.println("Inter重中的静态方法");
    }
}
class InterImpl implements Inter,Flyable{
    @Override
    public void show() {
        System.out.println("show方法");
    }
}

public class InterDemo {
    public static void main(String[] args) {
        Inter i = new InterImpl();
        i.show();
        i.method();
//        i.test();
        Inter.test();
//        InterImpl.test();
        Flyable.test();
    }
}

在这里插入图片描述

私有方法

在这里插入图片描述

public interface Inter {
    private void show() {
        System.out.println("初级工程师");
        System.out.println("中级工程师");
        System.out.println("高级工程师");
    }
    private static void method() {
        System.out.println("初级工程师");
        System.out.println("中级工程师");
        System.out.println("高级工程师");
    }
    default void show1(){
        System.out.println("show1开始执行");
        show();
//        method();//也可以调用
        System.out.println("show1结束执行");
    }
    default void show2(){
        System.out.println("show2开始执行");
        show();
        System.out.println("show2结束执行");
    }
    static void method1(){
        System.out.println("method1开始执行");
//        show();//不能调用非静态方法
        method();
        System.out.println("method1结束执行");
    }
    static void method2(){
        System.out.println("method2开始执行");
        method();
        System.out.println("method2结束执行");
    }
}

class InterImpl implements Inter{
}

public class InterDemo {
    public static void main(String[] args) {
        Inter i = new InterImpl();
        i.show1();
        System.out.println("---------------");
        i.show2();
        System.out.println("---------------");
        Inter.method1();
        System.out.println("---------------");
        Inter.method2();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿炳的旅程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值