您的位置:首页 > 其它

类的静态变量访问

2016-04-16 23:29 351 查看
<?php

/**
* @author keke
* @copyright 2016
* 这是一个类的标准格式,用于记忆,静态变量访问:student::$fee
*/

class student
{
public static $fee = 0;
public $name;
function __construct($name)
{
$this->name = $name; //非静态属性访问格式:$this->name
}

public static function runCode($fee)
{
student::$fee += $fee; //这里不能用$this->fee非静态格式访问,是静态变量访问格式:student::$fee
}

public function returnStr()
{
return student::$fee; //这里不能用$this->fee非静态格式访问,是静态变量访问格式:student::$fee
}

}

$zhangsan = new student("张三");
$zhangsan->runCode(320);

$lisi = new student("李四");
$lisi->runCode(560);
//这里不能用( 类::方法 )来访问
echo $lisi->returnStr(); //结果:880

echo '<br />运行完成!';
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: