类的继承用在商品上,基类和继承类的应用

<?php

class Goods //基类Goods
{
    private $name;
    private $price;

    protected function __construct($name2,$price2)
    {
        $this->name = $name2;
        $this->price = $price2;
    }
    public function showInfo()
    {
        $str = '产品的名称是:'.$this->name;
        $str .= '<br>产品的价格是:'.$this->price;
        return $str;
    }
}

class Computer extends Goods //电脑类继承Goods类
{
    private $color;//电脑类的新的属性
    private $cpu;//电脑类的新的属性

    public function __construct($name2,$price2,$color2,$cpu2)
    {
        parent::__construct($name2,$price2);//给父类的name price赋值 用parent调用父类
        $this->color = $color2;//正常赋值
        $this->cpu = $cpu2;//正常赋值
    }
    public function showInfo()//调用
    {
    $str = parent::showInfo();//调用父类的方法
    $str .= '<br>颜色是:'.$this->color;
    $str .= '<br>cpu的型号是:'.$this->cpu;
    return $str;
    }
}
$computer = new Computer('联想',4600,'黑色','M-skr');
echo $computer->showInfo();

继承父类后,子类可以用父类的所有东西,可以大展拳脚.释放自己

转载于:https://www.cnblogs.com/xm666/p/11265311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值