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 关注
- 128 浏览
添加回答
举报