PHP面向對象練習
阿新 • • 發佈:2017-07-17
ted ext php aic 練習 alt com 攔截 fun
根據描述,按照面向對象的方式來定義類或者接口:
1 交通警察執法攔截闖紅燈的奧迪汽車
2 刑警於今日7月17日淩晨抓捕了江蘇常熟縱火案嫌疑犯
3 青蛙一般在田間捕捉害蟲;蜻蜓能飛,一般在空中捕捉害蟲;啄木鳥也能飛,一般在樹上捕捉害蟲
/**
* 青蛙一般在田間捕捉害蟲;蜻蜓能飛,一般在空中捕捉害蟲;啄木鳥也能飛,一般在樹上捕捉害蟲;
*/
abstract class DongWu{
protected $mingZi;
protected $diDian;
public function __construct($mingZi, $diDian){
$this->diDian = $diDian;
$this->mingZi = $mingZi;
}
public function zhuoHaiChong(){
echo $this->mingZi.$this->diDian.‘捕捉害蟲‘;
}
}
interface Fly{
public function fly();
}
class QingWa extends DongWu{
// public function zhuoHaiChong(){
// echo $this->mingZi.$this->diDian.‘捕捉害蟲‘;
// }
}
class QingTing extends DongWu implements Fly {
public function fly(){
return $this->mingZi.‘能飛,‘;
}
public function zhuoHaiChong(){
echo $this->fly();
parent::zhuoHaiChong();
}
}
class ZhuoMuNiao extends DongWu implements Fly{
public function fly(){
return $this->mingZi.‘能飛,‘;
}
public function zhuoHaiChong(){
echo $this->fly();
parent::zhuoHaiChong();
}
}
$qw = new QingWa(‘青蛙‘, ‘在田間‘);
$qw->zhuoHaiChong();
echo ‘<br><br>‘;
$qt = new QingTing(‘蜻蜓‘, ‘在空中‘);
$qt->zhuoHaiChong();
echo ‘<br><br>‘;
$zmn = new QingTing(‘啄木鳥‘, ‘在樹上‘);
$zmn->zhuoHaiChong();
PHP面向對象練習