PHP USES interface abstract class common base class to implement 'interface oriented programming' and 'coupling method'

  • 2020-03-31 21:33:35
  • OfStack

 
<?php 
 
//---------------------------------------------------------------------- 
 
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 " I am flash memory: <br>"; 
} 
function read(){ 
echo " Start reading ......<br>"; 
} 
function write(){ 
echo " Start storing data ......<hr>"; 
} 
} 
class yingpan implements readandwrite{ 
function __construct(){ 
echo " I am a hard drive: <br>"; 
} 
function read(){ 
echo " Start reading ......<br>"; 
} 
function write(){ 
echo " Start storing data ......<hr>"; 
} 
} 
class disco implements readandwrite{ 
function __construct(){ 
echo " I am CD: <br>"; 
} 
function read(){ 
echo " Start reading ......<br>"; 
} 
function write(){ 
echo " Start storing data ......<hr>"; 
} 
} 
//---------------------------------------------------------------------- 
 
//---------------------------------------------------------------------- 
//[mode 3] general class inheritance implementation mode:
 
//---------------------------------------------------------------------- 
 
//---------------------------------------------------------------------- 
 
/* 
class unknow{ 
function __construct(){ 
echo "<font color=#ff0000> I am an alien memory unknown to earthmen, and I have access methods different from those of earth: </font><br>"; 
} 
function Rdata(){ 
echo "I'm reading now......<br>"; 
} 
function Wdata(){ 
echo "I'm writing now......<hr>"; 
} 
} 
class Adpater extends readandwrite{ 
private $obj; 
function __construct(unknow $x){ 
$this->obj=$x; 
} 
function read(){ 
$this->obj->Rdata(); 
} 
function write(){ 
$this->obj->Wdata(); 
} 
} 
*/ 
//---------------------------------------------------------------------- 
/* 
 [method 2]  
class unknow{ 
function __construct(){ 
echo "<font color=#ff0000> I am an alien memory unknown to earthmen, and I have access methods different from those of earth: </font><br>"; 
} 
function Rdata(){ 
echo "I'm reading now......<br>"; 
} 
function Wdata(){ 
echo "I'm writing now......<hr>"; 
} 
} 
class Adpater extends unknow implements readandwrite{ 
function read(){ 
parent::Rdata(); 
} 
function write(){ 
parent::Wdata(); 
} 
} 
*/ 
//------------------------------------------------------------------------ 
 
class unknow{ 
function __construct(){ 
echo "<font color=#ff0000> I am an alien memory unknown to earthmen, and I have access methods different from those of earth: </font><br>"; 
} 
function Rdata(){ 
echo "I'm reading now......<br>"; 
} 
function Wdata(){ 
echo "I'm writing now......<hr>"; 
} 
} 
class Adpater implements readandwrite{ 
private $obj; 
function __construct(unknow $x){ 
$this->obj=$x; 
} 
function read(){ 
$this->obj->Rdata(); 
} 
function write(){ 
$this->obj->Wdata(); 
} 
} 
//[program body call]
echo "<strong><font color=#990000 size=20px> Object-oriented programming -- interfaces </font></strong><hr>"; 
$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(); 
?> 

Related articles: