zoukankan      html  css  js  c++  java
  • JAVA之接口与实现

    /**
     *
     * 功能:接口与实现
     * 接口也体现了多态性
     */
    package com.test;

    public class test5 {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub

            //创建computer对象
            Computer computer = new Computer();
            //创建camera对象
            Camera camera = new Camera();
            //创建phone对象
            Phone phone = new Phone();
            
            //通过接口调用其对应的方法
            computer.useUsb(camera);
            computer.useUsb(phone);
        }

    }

    //Usb接口
    interface Usb
    {
        //开始函数
        public void start();
        //停止函数
        public void stop();
    }

    //相机类,实现Usb接口
    class Camera implements Usb
    {
        public void start()
        {
            System.out.println("相机开始工作了!!");
        }
        
        public void stop()
        {
            System.out.println("相机停止工作!!");
        }
    }

    //手机类,实现Usb接口
    class Phone implements Usb
    {
        public void start()
        {
            System.out.println("手机开始工作!!");
        }
        
        public void stop()
        {
            System.out.println("手机停止工作!!");
        }
    }

    //计算机
    class Computer
    {
        //开始使用Usb接口
        public void useUsb(Usb usb)
        {
            usb.start();
            usb.stop();
        }
    }

  • 相关阅读:
    js/jquery 页面传值
    php 连接sqlserver方法
    php 写webservice常见问题
    php 解决json_encode中文null和UNICODE转码问题
    手机web——自适应网页设计
    50个js技巧(分享)
    php webservice客户端和服务器端
    php 文件下载功能
    es6 字符串的扩展
    vue $emit抛出事件
  • 原文地址:https://www.cnblogs.com/milantgh/p/4036921.html
Copyright © 2011-2022 走看看