靜態方法調用靜態變量的問題。。 return $speed+10;為什么錯的??一定要寫return self::$speed+=10;?
class Car {
? ? private static $speed = 10;? ??
? ? public function getSpeed() {
? ? ? ? return self::$speed;
? ? }??
? ? //在這里定義一個靜態方法,實現速度累加10
public static function speedUp(){
? ? return $speed+10;
}
}
$car = new Car();
Car::speedUp(); ?//調用靜態方法加速$speed+10
echo $car->getSpeed(); ?//調用共有方法輸出當前的速度值
2017-01-05
靜態變量是整個類的,靜態方法只能訪問靜態變量,只能使用self或static關鍵字訪問