Blogger Information
Blog 31
fans 0
comment 0
visits 24408
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Day34-2018/1/9(写一个父类一个子类)
SmallKing的博客
Original
763 people have browsed it

写一个父类一个子类,练习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();

QQ图片20180110195111.png

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post