首页 > 代码库 > PHP面向对象练习

PHP面向对象练习

根据描述,按照面向对象的方式来定义类或者接口:

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面向对象练习