[PHP面向对象]④--访问控制private\protected

Paste_Image.png
<?php
//peoples republic of china
date_default_timezone_set("PRC");

class Human
{
    public $name;
    public $weight;
    protected $height;//只有自身和子类可以访问
    private $isHungry = true;//不能被子类访问

    public function isHungry()
    {
        echo $this->name . " is Hungry? " . $this->isHungry;
    }
}

class NbaPlayer extends Human
{
    public $team = "Bull";
    public $playerNumber = "23";
    private $age = 40;

    function __construct($name, $height)
    {
        $this->name = $name;//this 伪变量
        $this->height = $height;
    }

    public function getAge()
    {
        echo $this->name . "'s age is " . ($this->age - 2);
    }

}

$jordan = new NbaPlayer('hehe', '220cm');
$jordan->getAge();
echo "<br/>";
$jordan->isHungry();
//echo "<br/>".$jordan.$height;//无法访问
?>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容