echo $car->getSpeed(); 為何沒返回結果
speed; } protected function speedUp() {$this->speed += 10; ? ?} ? ?public function start(){ ? ? ? return $this->speedUp(); ? ?} } $car = new Car(); echo $car->start(); echo $car->getSpeed(); 為何顯示的結果只有20 ?下面那個10去哪里了
speed; } protected function speedUp() {$this->speed += 10; ? ?} ? ?public function start(){ ? ? ? return $this->speedUp(); ? ?} } $car = new Car(); echo $car->start(); echo $car->getSpeed(); 為何顯示的結果只有20 ?下面那個10去哪里了
2016-04-28
舉報
2016-05-27
你要在getSpeed()函數中把值return就可以了
protected function speedUp() {
? ? ? ? return $this->speed += 10;
}
這樣就可以輸出了。
2016-04-28
你這個代碼好像不全吧。
2016-04-28
我看了一下你提交的代碼,應該是?private $speed = 0;而不是?private $speed = 10;
注意:賦值的是0,而不是10.