<?php header('content-type:text/html;charset=utf-8'); class ZiXingChe{ protected $name = '自行车'; protected $gulu = 2; protected $zuodeng = 1; protected $jiaotaban = 2; protected $yanse; public function qiche(){ echo '可以骑动'; } public function shache(){ echo '可以刹车制动'; } public function jieshao(){ echo '这个是'.$this->name; echo ','; $this->qiche(); echo ','; $this->shache(); } } //继承: 扩展父类的内容,注意只能继承public和protected修饰的内容,而private修饰的则不能继承 // 关键字: extends // PHP中继承是单继承,即extends后面只能有1个类名;但是1个类可以有无数个子类 // 子类和父类只能出现在有继承关系的2个类之间 class ZheDieZiXingChe extends ZiXingChe{ public function __construct($name = '折叠自行车'){ $this->name = $name; } public function zhedie(){ echo $this->name.'可以折叠'; } public function jieshao(){ echo '这个是'.$this->name; echo ','; $this->qiche(); echo ','; $this->shache(); echo ','; $this->zhedie(); } } class DianDongZiXingChe extends ZiXingChe{ private $dianchi = 1; public function __construct($name = '电动自行车'){ $this->name = $name; } public function diandongxingshi(){ echo $this->name.'可以电动行驶'; } public function jieshao(){ echo '这个是'.$this->name; echo ','; $this->qiche(); echo ','; $this->shache(); echo ','; $this->diandongxingshi(); } } $putong = new ZiXingChe(); $putong->jieshao(); echo '<br><br>'; $zhedie = new ZheDieZiXingChe(); $zhedie->jieshao(); echo '<br><br>'; $diandong = new DianDongZiXingChe(); $diandong->jieshao();
PHP面向对象-特征之一:继承
最新推荐文章于 2022-10-16 10:04:17 发布