面向对象的写码

it2026-04-14  3

<?php

/***鸟能在空中飞;飞机也能在空中飞;超人也能在空中飞;*空警能管理和控制飞机的飞行线路,当然也能管理超人的飞行线路,但是对于鸟是无法管理的 */

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();

转载于:https://www.cnblogs.com/ZJCD/p/7206479.html

最新回复(0)