静态属性静态方法

<?php
/*静态属性用于保存内的公有数据
 * 静态方法里面只能访问静态属性
 * 静态成员不需要实例化就可以访问
 * 类的内部可以通过self或者static关键字访问自身的静态成员
 * 子内方法中可以通过parent关键字访问父类的静态成员
 * 可以通过类的名称在类定义外部访问静态成员
 */


class Human{
public $name;
public $height;
public $weight;
    public static $value= "this value is ture ";
    static function valu(){
    echo self::$value;
    }
}
class NbaPlayer extends Human{
public $team;
public $playernumber;
public static $president= "DAVSID";
function __construct($name,$height,$weight,$team,$playernumber){
echo $this->name."jumping"."<br>";
$this->name=$name;
        $this->height=$height;
$this->weight=$weight;
$this->team=$team;
$this->playernumber=$playernumber;
}
function jump(){
echo $this->name."jump"."<br>";
echo parent::valu()."<br>";//parent关键字在非静态方法中同样适用

static function changpresident($newpresident){

echo static::$president."<br>";
echo self::$president."<br>";//类的内部可以通过self或者static关键字访问自身的静态成员
echo self::$president=$newpresident."<br>";
echo parent::$value."<br>";//用parent关键字访问父类中的静态属性
echo parent::valu();//用parent关键字访问父类中的静态方法
//echo $this->name;$this只能用于实例化对象中;
}
 

}
   NbaPlayer::changpresident("dad");//可以通过类的名称在类定义外部访问静态成员
$jodan=new NbaPlayer("jodan","203cm","120kg","bull","23");
echo $jodan->height;
$jodan->jump();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值