父类
<?php class Computer { protected $brand; protected $model; protected $price; //构造方法 public function __construct($brand,$model,$price) { $this->brand = $brand; $this->model = $model; $this->price = $price; } public function program() { return 'IE浏览器'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
子类
<?php class Lenovo extends Computer { public function __get($name) { return $this->$name; } //1.对父类属性进行拓展 private $bluetooth = false;//是否有蓝牙功能 private $usb3 = false;//USB3.0支持 private $handwriting = false; //手写功能 public function __construct($brand, $model, $price, $bluetooth, $usb3, $handwriting) { parent::__construct($brand, $model, $price); $this->bluetooth = $bluetooth; $this->usb3 = $usb3; $this->handwriting = $handwriting; } //2.增加新方法 public function restore() { return '支持一键还原'; } //3.类方法重写/重载 public function program() { return parent::program().'、联想电脑管家、Office'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例化子类
<?php //使用自动加载器加载类 spl_autoload_register(function ($classname){ require './class/'.$classname.'.php'; }); $lenovo = new Lenovo('Lenovo','Win10','5499',true,true,true); echo '品牌:'.$lenovo->brand.'<br>'; echo '系统:'.$lenovo->model.'<br>'; echo '价格:'.$lenovo->price.'元<br>'; echo '蓝牙:'.($lenovo->bluetooth ? '支持':'不支持').'<br>'; echo 'USB3.0:'.($lenovo->usb3 ? '支持':'不支持').'<br>'; echo '手写:'.($lenovo->handwriting ? '支持':'不支持').'<br>'; echo '预装软件:'.$lenovo->program().'<br>'; echo $lenovo->restore();
运行实例 »
点击 "运行实例" 按钮查看在线实例