php类的继承与方法重载
Car.php
实例
<?php /医院 * */ class Car { protected $price; protected $speed; public function __construct($price,$speed) { $this->price=$price; $this->speed=$speed; } public function drive (){ return 'drive the car'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
Jeep.php
实例
<?php /医院 * */ class Jeep extends Car { private $level; private $sales; public function __construct($price, $speed,$level,$sales) { parent::__construct($price, $speed); $this->level=$level; $this->sales=$sales; } public function drive() { return parent::drive().' jeep'; } public function __get($name) { return $this->$name; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
test.php
实例
<?php /医院 * */ spl_autoload_register(function ($className){ require './'.$className.'.php'; }); $jeep=new Jeep(200000,100,3,20000); echo 'price:'.$jeep->price,'<br>'; echo 'speed:'.$jeep->speed,'<br>'; echo 'level:'.$jeep->level,'<br>'; echo 'sales:'.$jeep->sales,'<br>'; echo $jeep->drive();
运行实例 »
点击 "运行实例" 按钮查看在线实例