复制代码 代码如下:
//----------------------------------------------------------------------
interface readandwrite{
function read();
function write();
}
class motherboard{
private $storage;
function __construct(readandwrite $obj){
$this->storage=$obj;
}
function read(){
$this->storage->read();
}
function write(){
$this->storage->write();
}
}
class flash implements readandwrite{
function __construct(){
echo "我是闪存:
";
}
function read(){
echo "开始读取数据......
";
}
function write(){
echo "开始储存数据......
";
}
}
class yingpan implements readandwrite{
function __construct(){
echo "我是硬盘:
";
}
function read(){
echo "开始读取数据......
";
}
function write(){
echo "开始储存数据......
";
}
}
class disco implements readandwrite{
function __construct(){
echo "我是光盘:
";
}
function read(){
echo "开始读取数据......
";
}
function write(){
echo "开始储存数据......
";
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//【方式三】一般类继承实现方式:
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//------------------------------------------------------------------------
class unknow{
function __construct(){
echo "我是地球人未知的外星储存器,我有不同于地球储存器的存取方式:
";
}
function Rdata(){
echo "I'm reading now......
";
}
function Wdata(){
echo "I'm writing now......
";
}
}
class Adpater implements readandwrite{
private $obj;
function __construct(unknow $x){
$this->obj=$x;
}
function read(){
$this->obj->Rdata();
}
function write(){
$this->obj->Wdata();
}
}
//【程序主体调用】
echo "面向对象程序设计——接口
";
$storage1=new flash();
$computer=new motherboard($storage1);
$computer->read();
$computer->write();
$storage2=new yingpan();
$computer=new motherboard($storage2);
$computer->read();
$computer->write();
$storage3=new disco();
$computer=new motherboard($storage3);
$computer->read();
$computer->write();
$un_storage=new unknow();
$apdaterx=new Adpater($un_storage);
$computer=new motherboard($apdaterx);
$computer->read();
$computer->write();
?>



