PHP 类的继承与方法重写

2018-09-30 10:28:35 来源: 扬州新竹网络科技有限公司
类的继承与方法重写
class Demo1
{
    父类属性
    private $name;
    public $age;
    protected $salary;
    const TXET_WORD='文本字';
  父类构造器
    public function __construct($name,$age,$salary)
    {
        $this->name=$name;
        $this->age=$age;
        $this->salary=$salary;
    }
  属性访问重载
    public function __get($name)
    {
        if (isset($this->$name)) {
            return $this->$name;
        }
        return '非法属性';
    }
}
class Demo_one extends Demo1{
    private $sex;
    const  TXET_WORD = '超级文本字';
    public function __construct($name,$age,$salary,$sex='women')
    {
       parent::__construct($name,$age,$salary,$sex);
       $this->sex=$sex;
    }
    public function __get($name)
    {
       if (isset($this->$name)) {
            return $this->$name;
        }
        return '非法属性';
    }
}
子类能继承父类的公共的,受保护的成员,不能继承私有的成员,也能继承常量
$demo_one=new Demo_one('张三',80,6000);
echo $demo_one->age;
echo $demo_one->name;
echo $demo_one->salary;
echo Demo_one::TXET_WORD;
echo $demo_one->sex;
本站文章均为新竹网站建设摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,我们欢迎您分享,引用和转载,我们谢绝直接复制和抄袭!感谢...