• 24.2 接口案例


    /*
    * 篮球运动员和教练
    乒乓球运动员和教练
    现在篮球运动员和教练要出国访问,需要学习英语
    请根据你所学的知识,分析出来哪些是类,哪些是抽象类,哪些是接口
    */

    为何学英语是接口?

    继承的特点是是单一继承,各个成员之间都有了对应关系。需要利用接口的多继承特点。

    package day24_1接口_interface;
    
    public class InterfaceTest {
        public static void main(String[] args) {
            BasketballPlayer bbp = new BasketballPlayer();
            bbp.name = "庆阳";
            bbp.age = 18;
            bbp.gender = "男";
            bbp.eat();
            bbp.study();
            bbp.speaking();
            System.out.println("-----------");
    
            PingpangCoach ppc = new PingpangCoach();
            ppc.age = 20;
            ppc.eat();
            ppc.teach();
    
        }
    }
    
    //
    class Person {
        String name;
        int age;
        String gender;
    
        public void eat() {
            System.out.println("吃饭");
        }
    
        public void sleep() {
            System.out.println("睡觉");
        }
    }
    
    //说英语
    interface speak {
        public abstract void speaking();
    }
    
    //运动员
    abstract class Player extends Person { //
        public abstract void study();
    }
    
    //教练
    abstract class Coach extends Person {
        public abstract void teach();
    }
    
    //篮球运动员
    class BasketballPlayer extends Player implements speak {
    
        @Override
        public void study() {
            System.out.println("学篮球");
        }
    
        @Override
        public void speaking() {
            System.out.println("说英语");
        }
    }
    
    //乒乓球运动员
    class PingpangPlayer extends Player {
    
        @Override
        public void study() {
            System.out.println("学乒乓");
        }
    }
    
    //篮球教练
    class BasketballCoach extends Coach implements speak {
    
        @Override
        public void teach() {
            System.out.println("教 篮球");
        }
    
        @Override
        public void speaking() {
            System.out.println("说英语");
        }
    }
    
    //乒乓球教练
    class PingpangCoach extends Coach {
    
        @Override
        public void teach() {
            System.out.println("教 乒乓");
        }
    }

    输出如下

  • 相关阅读:
    PHP中的类函数和类对象
    PHP魔术方法__clone()篇
    PHP魔术方法__tostring()篇
    PHP魔术方法__call()篇
    自己写的PHP的mql类
    PHP的分页
    ThinkPHP的调用css,js和图片的路径
    JavaScript作用域链
    在Eclipse中开发WEB项目
    eclipse官方网址、各个版本的下载
  • 原文地址:https://www.cnblogs.com/longesang/p/11226690.html
Copyright © 2020-2023  润新知