欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

4.5类的继承与代码复用

程序员文章站 2023-12-25 22:28:45
...
<?php
// 类的继承: 代码复用
class ParentClass
{
    public $name;
    protected $course;
    private $salary;
    const SITE_NAME = '长沙网站开发';
    //构造方法
    public function __construct($name,$course,$salary)
    {
        $this->name=$name;
        $this->course = $course;
        $this->salary = $salary;
    }
    //访问本类私有成员
    public function getSalary()
    {
        return $this->salary;
    }
}
class ChildClass extends ParentClass
{
//子类中 自动获取父类中的所有属性和方法,除了私有的
        //获取受保护的
    public function getCourse()
    {
        return $this->course;
    }
}
$chicl=new ChildClass('Devin','php','12000');
echo '姓名是:'.$chicl->name.'</br>'.'技能:'.$chicl->getCourse().'</br>'.'所属公司:'.ParentClass::SITE_NAME;
相关标签: 面对对象

上一篇:

下一篇: