• 非零基础自学Java (老师:韩顺平) 第10章 面向对象编程(高级部分) 10.8 接口


    非零基础自学Java (老师:韩顺平)

    ✈【【零基础 快速学Java】韩顺平 零基础30天学会Java】

    第10章 面向对象编程(高级部分)

    10.8 接口
    10.8.1 为什么有接口

    在这里插入图片描述

    10.8.2 接口快速入门

    一个程序就是一个世界,在现实世界中存在的情况,在程序中也会出现。

    举个栗子

    接口

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: UsbInterface
     * date: 2022/9/3 20:37
     *
     * @author DingJiaxiong
     */
    
    public interface UsbInterface { //接口
        //规定接口的相关方法,规范
        public void start();
        public void stop();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    相机类

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: Camera
     * date: 2022/9/3 20:37
     *
     * @author DingJiaxiong
     */
    
    public class Camera implements UsbInterface{//实现接口,就是把接口方法实现
        @Override
        public void start() {
            System.out.println("相机开始工作...");
        }
    
        @Override
        public void stop() {
            System.out.println("相机停止工作");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    手机类

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: Phone
     * date: 2022/9/3 20:39
     *
     * @author DingJiaxiong
     */
    
    public class Phone implements UsbInterface{
        @Override
        public void start() {
            System.out.println("手机开始工作...");
        }
    
        @Override
        public void stop() {
            System.out.println("手机停止工作...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    计算机类

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: Computer
     * date: 2022/9/3 20:41
     *
     * @author DingJiaxiong
     */
    public class Computer {
        public void work(UsbInterface usbInterface){
            //通过接口,调用方法
            usbInterface.start();
            usbInterface.stop();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试类

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: Interface01
     * date: 2022/9/3 20:39
     *
     * @author DingJiaxiong
     */
    
    public class Interface01 {
        public static void main(String[] args) {
            Camera camera = new Camera();
            Phone phone = new Phone();
    
            Computer computer = new Computer();
    
            computer.work(phone); //把手机接入计算机
            System.out.println("=====================");
            computer.work(camera); //将相机接入计算机
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    运行结果

    在这里插入图片描述

    10.8.3 基本介绍

    接口就是给出一些没有实现的方法,封装到一起,到某个类要使用的时候,在根据具体情况把这些方法写出来。

    语法:

    interface 接口名{
        //属性
        //抽象方法
    }
    
    class 类名 implements 接口{
        自己的属性;
        自己的方法;
        必须实现的接口的抽象方法;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    小结:

    接口是更加抽象的抽象类,抽象类里的方法可以有方法体,接口里的所有方法都没有方法体【jdk7.0】。

    接口体现了程序设计的多态和高内聚低偶合的设计思想。

    特别说明:Jdk8.0后接口类可以有静态方法,默认方法,也就是说接口中可以有方法的具体实现★

    10.8.4 深入讨论

    【几个应用场景】

    现在要制造战斗机,武装直升机.专家只需把飞机需要的功能/规格定下来即可,然后让别的人具体实现就可。

    在这里插入图片描述

    现在有一个项目经理(段玉),管理三个程序员,功能开发一个软件,为了控制和管理软件,项目经理可以定义一些接口,然后由程序员具体实现。

    在这里插入图片描述

    举个栗子

    接口

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: DBInterface
     * date: 2022/9/3 20:48
     *
     * @author DingJiaxiong
     */
    
    public interface DBInterface { //项目经理
        public void connect(); //连接方法
        public void close(); //关闭连接
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    A程序

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: MysqlDB
     * date: 2022/9/3 20:49
     *
     * @author DingJiaxiong
     */
    //A程序
    public class MysqlDB implements DBInterface{
        @Override
        public void connect() {
            System.out.println("连接MySQL");
        }
    
        @Override
        public void close() {
            System.out.println("关闭MySQL");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    B程序

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: OracleDB
     * date: 2022/9/3 20:50
     *
     * @author DingJiaxiong
     */
    //B 程序
    public class OracleDB implements DBInterface{
        @Override
        public void connect() {
            System.out.println("连接oracle");
        }
    
        @Override
        public void close() {
            System.out.println("关闭oracle");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试类

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: Interface03
     * date: 2022/9/3 20:50
     *
     * @author DingJiaxiong
     */
    
    public class Interface03 {
        public static void main(String[] args) {
            MysqlDB mysqlDB = new MysqlDB();
            t(mysqlDB);
    
            System.out.println("=====================");
    
            OracleDB oracleDB = new OracleDB();
            t(oracleDB);
        }
    
        private static void t(DBInterface dbInterface) {
            dbInterface.connect();
            dbInterface.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行结果

    在这里插入图片描述

    10.8.5 注意事项和细节
    • 接口不能被实例化

    • 接口中所有的方法是public方法,接口中抽象方法,可以不用abstract修饰

    • 一个普通类实现接口,就必须将该接口的所有方法都实现

    • 抽象类实现接口,可以不用实现接口的方法

    • 一个类同时可以实现多个接口

    • 接口中的属性只能是final的,而且是public static final 修饰符 (且必须初始化)

    • 接口中属性的访问形式:接口名.属性名

    • 接口不能继承其它的类,但是可以继承多个 别的接口

      interface A extends B , C{}
      
      • 1
    • 接口的修饰符只能是public 和 默认,这点和类的修饰符是一样的。

    10.8.7 实现接口 vs 继承类

    举个栗子

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: ExtendsVsInterface
     * date: 2022/9/3 20:58
     *
     * @author DingJiaxiong
     */
    
    public class ExtendsVsInterface {
        public static void main(String[] args) {
            LittleMonkey wuKong = new LittleMonkey("孙悟空");
            wuKong.climbing();
            wuKong.swimming();
            wuKong.flying();
        }
    }
    
    class Monkey{
        private String name;
    
        public Monkey(String name) {
            this.name = name;
        }
    
        public void climbing(){
            System.out.println(name + "会爬树...");
        }
    
        public String getName(){
            return name;
        }
    }
    
    interface Fishable{
        void swimming();
    }
    
    interface Birdable{
        void flying();
    }
    
    class LittleMonkey extends Monkey implements Fishable , Birdable{
    
    
        public LittleMonkey(String name) {
            super(name);
        }
    
        @Override
        public void swimming() {
            System.out.println(getName() + "通过学习,可以像鱼一样游泳");
        }
    
        @Override
        public void flying() {
            System.out.println(getName() + "通过学习,可以像鸟一样飞翔");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这里插入图片描述

    • 当子类继承了父类,就自动拥有了父类的功能
    • 如果子类需要扩展功能,可以通过实现接口的方式进行扩展
    • 【实现接口】 是 对java单继承机制的一种补充

    【接口和继承解决的问题不同】

    继承的价值主要在于:解决代码的复用性和可维护性。

    接口的价值主要在于:设计,设计好各种规范(方法),让其它类去实现这些方法。即更加的灵活…

    【接口比继承更加灵活】

    接口比继承更加灵活,继承是满足is - a的关系,而接口只需满足like - a的关系。

    【接口在一定程度上实现代码解耦[即:接口规范性+动态绑定机制]】

    10.8.8 接口的多态特性

    【多态参数】

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: InterfacePolyParameter
     * date: 2022/9/3 21:06
     *
     * @author DingJiaxiong
     */
    
    public class InterfacePolyParameter {
        public static void main(String[] args) {
            //接口的多态体现
            //接口类型的变量if01可以指向实现了IF接口类的对象实例
            IF if01 = new Monster();
            if01 = new Car();
    
            //继承体现的多态
            //父类类型的变量 a 可以指向 继承AAA的子类的对象实例
            AAA a = new BBB();
            a = new CCC();
    
        }
    }
    
    interface IF{}
    
    class Monster implements IF{}
    
    class Car implements IF{}
    
    class AAA{}
    
    class BBB extends AAA{}
    
    class CCC extends AAA{}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    【多态数组】

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: InterfacePolyArr
     * date: 2022/9/3 21:10
     *
     * @author DingJiaxiong
     */
    
    public class InterfacePolyArr {
        public static void main(String[] args) {
            //多态数组 → 接口类型数组
            Usb[] usbs = new Usb[2];
            usbs[0] = new Phone();
            usbs[1] = new Camera();
    
            for (int i = 0; i < usbs.length; i++) {
                usbs[i].work(); //动态绑定
                //向下转型
                if (usbs[i] instanceof Phone){
                    ((Phone) usbs[i]).call();
                }
            }
    
        }
    }
    
    interface Usb{
        void work();
    }
    
    class Phone implements Usb{
    
        public void call(){
            System.out.println("手机可以打电话...");
        }
    
        @Override
        public void work() {
            System.out.println("手机工作中...");
        }
    }
    
    class Camera implements Usb{
    
        @Override
        public void work() {
            System.out.println("相机工作中...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    运行结果

    在这里插入图片描述

    【接口存在多态传递现象】

    package com.dingjiaxiong.interface_;
    
    /**
     * ClassName: InterfacePolyPass
     * date: 2022/9/3 21:15
     *
     * @author DingJiaxiong
     */
    
    public class InterfacePolyPass {
        public static void main(String[] args) {
    
            接口类型的变量可以指向,实现了该接口的类的对象实例
            IG ig = new Teacher();
    
            //如果 IG 继承了 IH 接口,而 Teacher 类实现了 IG 接口
            //那么,实际上就相当于 Teacher 类也实现了 IH 接口. //这就是所谓的 接口多态传递现象.
    
            IH ih = new Teacher();
        }
    }
    
    interface IH{
        void hi();
    }
    
    interface IG extends IH{}
    
    class Teacher implements IG{
    
        @Override
        public void hi() {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    云计算中的过度授权:安全隐患与应对策略
    【高危安全通告】微软8月多个漏洞修复
    Freeswitch实现软电话功能
    基于EasyExcel锁定指定列导出数据到excel
    Vuex入门
    Android - toolbar 优化 title修改边距和navigation icon修改padding值
    如何设计神经网络结构图,神经网络设计与实现
    直接在 PI PO ESR 中编写 Java Mapping JAVA映射
    基于Springboot+vue的汽车租赁系统 elementui
    flink 状态
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126913665