实例 父类肉
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/6 0006 * Time: 21:03 */ class meat { protected $where; protected $price; protected $project; public function __construct($where,$price,$project) { $this->price = $price; $this->project = $project; $this->where = $where; } public function eat() { return '补充能量'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 子类牛肉
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/6 0006 * Time: 21:09 */ class beef extends meat { public function __get($name) { // TODO: Implement __get() method. return $this->$name; } //对父类属性进行拓展 private $eat = false; public function __construct($where, $price, $project, $eat) { parent::__construct($where, $price, $project); $this->eat = $eat; } //增加功能 public function cook() { return '可烧烤,可热炒。'; } //重载 public function eat() { return parent::eat().',增加力量。'; // TODO: Change the autogenerated stub return '等等'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/5/6 0006 * Time: 20:24 */ //require './class/user.php'; require './class/meat.php'; require './class/beef.php'; //$fg = new user(); //echo '姓名:'.$fg->name,'<br>'; //echo '性别:'.$fg->sex,'<br>'; //echo '年龄:'.$fg->age,'<br>'; //echo '身高:'.$fg->height; //$fg1 = new user('麦克','男',24,180); //echo '姓名:'.$fg1->name,'<br>'; //echo '性别:'.$fg1->sex,'<br>'; //echo '年龄:'.$fg1->age,'<br>'; //echo '身高:'.$fg1->height; $fg1 = new beef('china','beef',29,true); echo '产地:'.$fg1->where.'<br>'; echo '种类:'.$fg1->project.'<br>'; echo '价格:'.$fg1->price.'<br>'; echo '是否可食:'.($fg1->eat? '可食' : '不可以').'<br>'; echo $fg1->eat().'<br>'; echo $fg1->cook().'<br>';
运行实例 »
点击 "运行实例" 按钮查看在线实例