父类代码:
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/6 0006 * Time: 上午 6:42 */ class Tv { protected $brand; protected $place; protected $price; public function __construct($brand,$place,$price) { $this->brand=$brand; $this->place=$place; $this->price=$price; } public function feature(){ return '我的特点是能看有线电视'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
子类代码:
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/6 0006 * Time: 上午 6:48 */ class Smart_tv extends Tv { protected $func; protected $repair; //创建查询器 public function __get($name) { return $this->$name; } //扩展功能 public function __construct($brand, $place, $price,$func,$repair) { parent::__construct($brand, $place, $price); $this->func=$func; $this->repair=$repair; } //调用父类并改写 public function feature() { return parent::feature().',还能上网看视频。'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
前台页面:
实例
<?php //使用自动加载器 spl_autoload_register(function($className){ require $className.'.php'; }); $Smart_tv=new Smart_tv('三星','韩国','5000','无线连接','三年'); echo '品牌:'.$Smart_tv->brand.'<br>'; echo '产地:'.$Smart_tv->place.'<br>'; echo '价格:'.$Smart_tv->price.'<br>'; echo '功能:'.$Smart_tv->func.'<br>'; echo '保修:'.$Smart_tv->repair.'<br>'; //输出父类调用改写语句 echo $Smart_tv->feature();
运行实例 »
点击 "运行实例" 按钮查看在线实例