写一个父类一个子类,练习extends,public,private,protected的用法
商品信息类
<?php class goods //创建商品类 { public $name; public $price; protected $unit; //商品计量单位 private $kinds; //商品品种 public function __construct($name='大米',$price='3元',$unit='斤',$kinds='袁荣平杂交水稻特等品') { $this->name=$name; $this->price=$price; $this->unit=$unit; $this->kinds=$kinds; } public function getInfo() //获取商品信息 { return '商品名称:'.$this->name.'<br>商品价格:'.$this->price.'/'.$this->unit.'<br>'; } public function getKinds() { return '品种:'.$this->kinds.'<br>'; } } class drinks extends goods //创建商品类子类饮料类 { public $spec; public function __construct($name='可口可乐',$price='3元',$unit='瓶',$spec='330ml') { parent::__construct($name,$price,$unit); $this->spec=$spec; } public function getInfo() //获取商品信息 { return parent::getInfo().'规格:'.$this->spec.'<br>'; } } echo (new goods())->getInfo().(new goods())->getKinds(); echo '<hr>'; $beef=new goods('牛肉干','68元','斤','湖岭牛肉'); echo $beef->getInfo().$beef->getKinds(); echo '<hr>'; echo (new drinks())->getInfo(); echo '<hr>'; echo (new drinks('红牛','6元','瓶','300ml'))->getInfo();