1 回答

TA貢獻1829條經驗 獲得超6個贊
我不完全確定你要做什么,但作為學習練習,你只需要echo循環執行后的數字,所以它是10。要獲取每個數字,您需要echo在循環中使用它。其中之一,盡管這可能不是總體上最好的模式:
要么擺脫getCounter:
class Counter{
public $number;
public function setCounter($number){
$this->number = $number;
for($this->number ;$this->number < 10 ; $this-> number ++){
echo $this->number;
}
}
}
$counter= new Counter();
$counter->setCounter(0);
或者在循環中調用它:
class Counter{
public $number;
public function setCounter($number){
$this->number = $number;
for($this->number ;$this->number < 10 ; $this-> number ++){
$this->getCounter();
}
}
public function getCounter(){
echo $this->number;
}
}
$counter= new Counter();
$counter->setCounter(0);
和/或添加一個count方法:
class Counter{
public $number;
public function setCounter($number){
$this->number = $number;
}
public function getCounter(){
echo $this->number;
}
public function count() {
for($this->number ;$this->number < 10 ; $this-> number ++){
$this->getCounter();
}
}
}
$counter= new Counter();
$counter->setCounter(0);
$counter->count();
- 1 回答
- 0 關注
- 156 瀏覽
添加回答
舉報