private和protected修饰属性的区别

private 修饰的属性不能被继承和重写,protected可以

class Person
{
    private $name = 'KaiNan';
    protected $age = 22;

    public function getAge() {
        echo $this->age,'
'; echo $this->name; } } class Stu extends Person { private $name = 'NuoKe'; protected $age = 18; public function intro() { parent::getAge(); } } $lucy = new Stu(); $lucy->intro(); /* 18 KaiNan */

你可能感兴趣的:(private和protected修饰属性的区别)