PHP中多态,抽象类,接口,

PHP中多态,抽象类,接口,PHP中多态,抽象类,接口,小例子:

需求:公司定义一个接口让我们开发功能

  usb.interface.php: 

 <?php
interface USB{ public function run();
}

    store.class.php:

 <?php
include_once("./usb.interface.php");
class store implements USB{ public function run(){
$this -> initialize();
} private function initialize(){
echo "store running ..";
}
}

  mouse.class.php:

 <?php
include_once("./usb.interface.php");
class mouse implements USB{ public function run(){
$this -> init();
} public function init(){
echo "mouse running ...";
}
}

  key.class.php:

<?php
include_once("./usb.interface.php");
class key implements USB{ public function run(){
$this -> init();
} public function init(){
echo "key running ..";
}
}

使用:computer.class.php

<?php
include("./mouse.class.php");
include("./store.class.php");
include("./key.class.php"); class computer{ public function useUSB($obj){
$obj -> run();
}
} $computer = new computer(); $computer -> useUSB(new mouse());
echo "<hr />";
$computer -> useUSB(new store());
echo "<hr />";
$computer -> useUSB(new key());
上一篇:mysql免安装版安装


下一篇:JQuery File Upload 插件 出现 “empty file upload result” 错误的解决方案。