接口可以被类单实现,也可以被类多实现(加逗号)image.png
    接口在这里类似于父类(自带抽象方法,和常量),被子类实现
    image.png

    1. package com.itheima.d11_interface_implements;
    2. public class Test {
    3. public static void main(String[] args) {
    4. // 目标:理解接口的基本使用:被类实现
    5. PIngPongMan p = new PIngPongMan("张继科");
    6. p.run();
    7. p.competition();
    8. p.rule();
    9. }
    10. }
    1. // 这是运动员的接口类
    2. package com.itheima.d11_interface_implements;
    3. /**
    4. * 接口有规范
    5. */
    6. public interface SportMan {
    7. void run(); // 默认的抽象方法
    8. void competition(); // 中文名:比赛
    9. }
    10. // 这是法律Law接口类
    11. package com.itheima.d11_interface_implements;
    12. public interface Law {
    13. void rule(); // 遵纪守法
    14. }
    package com.itheima.d11_interface_implements;
    
    public class PIngPongMan implements SportMan,Law{// 用类实现接口
        private String name;
        // 这是一个有参构造器,传入的参数,赋值给该类的成员
        public PIngPongMan(String name){
            this.name = name;
        }
    
        @Override // 由于接口是抽象方法,所以必须重写抽象方法
        public void run() {
            System.out.println(name + "必须跑步训练!");
        }
    
        @Override
        public void competition() {
            System.out.println(name + "要参加比赛,为国争光");
        }
    
        @Override
        public void rule() {
            System.out.println(name + "遵纪守法");
        }
    }