[PHP面向对象]⑤--static

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 static $sValue = "sValue";

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

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

    static public $president = "David Stern";

    static public function changePresident($newPresdt)
    {
        self::$president = $newPresdt;
        echo "parent sValue" . parent::$sValue . "<br/>";
    }

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

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

}

echo NbaPlayer::$president . '<br/>';
NbaPlayer::changePresident('changepre');
echo NbaPlayer::$president . "<br/>";
echo Human::$sValue;

?>
Paste_Image.png
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容