PHP面向对象之封装和继承

一、面向对象-类的常规写法

// 定义学生类
class Student
{
    public $name;
    public $age;
    public $sex;
    public $score; // 成绩
    
    public function __construct($name, $age, $sex, $score)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        $this->score = $score;
    }
    
    public function printInfo()
    {
        echo '名字:'.$this->name.'<br>';
        echo '年龄:'.$this->age.'<br>';
        echo '性别:'.$this->sex.'<br>';
        echo '成绩:'.$this->score.'<br>';
    }
}

// 定义工人类
class Worker
{
    public $name;
    public $age;
    public $sex;
    public $salary; // 工资
    
    public function __construct($name, $age, $sex, $salary)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        $this->salary = $salary;
    }
    
    public function printInfo()
    {
        echo '名字:'.$this->name.'<br>';
        echo '年龄:'.$this->age.'<br>';
        echo '性别:'.$this->sex.'<br>';
        echo '工资:'.$this->salary.'<br>';
    }
}

// 实例化 Student类,并传值
$stu = new Student('jack', 18, '男', 84);
// 调用 Worker类中的 printInfo 方法
$stu->printInfo();

echo '<hr>';

// 实例化 Worker类,并传值
$worker = new Worker('tom', 17, '男', 8000);
// 调用 Worker类中的 printInfo方法
$worker->printInfo();

运行结果:

普通类

我们可以看到,Student类 和 Worker类中有很多相同的属性和方法,因此,我们可以把相同的部分提取出来,定义一个新的类来承载它(即父类),然后让 Student类 和 Worker类去继承它(Student类 和 Worker类 为子类),通过调用父类中的属性和方法来实现我们的需求,这样可以使代码简化很多。执行效果如下:类的封装和继承

二、面向对象-类的封装和继承

// 定义父类 Person类
class Person
{
    public $name;
    public $age;
    public $age;
    
    public function __construct($name, $age, $sex)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
    }
    
    public function printInfo()
    {
        echo '名字:'.$this->name.'<br>';
        echo '年龄:'.$this->age.'<br>';
        echo '性别:'.$this->sex.'<br>';
    }
}

// Student类 继承 Person父类
class Student extends Person
{
    public $score; // 成绩
    
    public function __construct($name, $age, $sex, $score)
    {
        // 调用父类中的构造方法
        parent::__construct($name, $age, $sex);
        $this->socre = $score;
    }
    
    public function printInfo()
    {
        // 调用父类中的普通方法
        parent::printInfo();
        echo '成绩:'.$this->score.'<br>';
    }
}

// Worker类 继承 Person父类
class Worker extends Person
{
    public $salary; // 工资
    
    public function __construct($name, $age, $sex, $salary)
    {
        // 调用父类中的构造方法
        parent::__construct($name, $age, $sex);
        $this->salary = $salary;
    }
    
    public function printInfo()
    {
        // 调用父类中的普通方法
        parent::printInfo();
        echo '工资:'.$this->salary.'<br>';
    }
}

$stu = new Student('jack', 18, '男', 84);
$stu->printInfo();

echo '<hr>';

$worker = new Worker('tom', 17, '男', 8000);
$worker->printInfo();

运行结果:

在这里插入图片描述

三、注意事项

1) 如果两个或多个子类中有相同的方法和属性,可以定义到父类中,让子类继承父类;
2) 在子类中调用父类的构造方法;
parent::__construct($name, $age, $sex); // 如果父类中有参数,则子类调用的时候也要写
3)在子类中调用父类的普通方法;
parent::方法名();

猜你喜欢

转载自blog.csdn.net/studyphp123/article/details/83050863
今日推荐