zoukankan      html  css  js  c++  java
  • 跟着百度学PHP[4]OOP面对对象编程-12-对象接口技术(interface)

    PHP与大多数面向对象编程语言一样,不支持多重继承。也就是说每个类只能继承一个父类

    接口正是解决每个类只能继承一个父类这个问题的

    接口用什么权限,继承的那个方法也要使用什么权限。

    接口的声明使用:interface

    接口的继承使用:implements

    目录++++++++++++++++++++++++++++++++++++++++++++

    00x1 接口的声明以及接口的引用(案例一)

    00x2 如何继承多个接口(案例二)

    00x3 判断某个对象是否实现了某个接口(案例三)

    ++++++++++++++++++++++++++++++++++++++++++++++

    00x1 案例一

    <?php 
    interface icaneat{           #使用interface声明一个接口
        public function eat($food); //接口里面不需要有方法的实现
    }
    class Human implements icaneat{      #使用implements继承接口(不能使用extends),稍后要与接口对接的属性或者方法要与其一致。
        public function eat($food){                            #如该行所示,要与接口的属性或者方法一致。
            echo "i eating ".$food."
    ";       //实现了某一个接口之后,必须提供接口中定义的方法的具体实现。
        }23
    }
    $test=new Human();
    $test->eat('apple');
     ?>
    输出效果如下:
    i eating apple

     00x2 案例二

    在implments使用后用逗号隔开即可实现对多个接口的继承。

    格式:

      implements 接口1,接口2,....

    <?php 
    interface icaneat{
        public function eat($food);
    }
    interface hello{
        public fuction hello($nihao);
    }
    class Human implements icaneat,hello{       #使用逗号隔开即可实现对多个接口的继承
        public function eat($food){
            echo "i eating ".$food."
    ";
        }
    }
    $test=new Human();
    $test->eat('apple');
    
     ?>

      00x3 案例三

    使用instanceof关键词检验

    范例:var_dump($object instanceof hello); #对象$object是否实现了hello接口

    <?php 
    interface test
    {
        public function one($a);
    }
    interface test2
    {
        public function two($b);
    }
    class chengdaniu implements test,test2
    {
        public function one($a){
            echo "我爱WEB安全技术!";
        }
        public function two($b){
            echo "我要成大牛!";
        }
    }
    $shi=new chengdaniu();
    var_dump($shi instanceof test);
     ?>
    输出效果如下所示:
    boolean true

    THE END


  • 相关阅读:
    国际标准:ISO 8601
    Python中文问题SyntaxError
    主人与狗
    三星Galaxy note I9220 N7000 刷机ROOT工具及ROM下载
    电子书下载:Pro ASP.NET 4 in C# 2010
    Rad Studio IDE 代码编辑器增强工具 RADSplit
    电子书下载:Essential Windows Phone 7.5
    3D成像法:抖动
    减小delphi生成EXE文件体积的方法
    unisynedit 在Delphi 2010下的编译问题
  • 原文地址:https://www.cnblogs.com/xishaonian/p/6158205.html
Copyright © 2011-2022 走看看