Home >Backend Development >PHP Tutorial > 写了段代码,不知道该怎么理解面向对象的继承

写了段代码,不知道该怎么理解面向对象的继承

WBOY
WBOYOriginal
2016-06-13 12:35:26831browse

写了段代码,不知道该如何理解面向对象的继承

本帖最后由 chaoxi1991 于 2013-10-18 16:36:27 编辑
class ParentClass {<br />
    private $private = 1;<br />
    public function getPrivate()<br />
    {<br />
        echo 'getPrivate() belong to class "' . get_class($this) . '"<br>';<br />
        return $this->private;<br />
    }<br />
}<br />
<br />
class Son extends ParentClass {<br />
}<br />
<br />
$son = new Son();<br />
echo 'private=' . $son->getPrivate();

我预期结果是应该报错的,但并没有报错。
执行结果是:
in class "ParentClass" function getPrivate():"Son"
private=1
想问为什么$private私有属性会被打印出来呢
PHP 面向对象 继承
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn