라이브러리
[PHP] NoRewindIterator::current - 현재 값을 가져옵니다.
NoRewindIterator::current
PHP의 `NoRewindIterator` 클래스는 `Iterator` 인터페이스를 구현한 클래스입니다. 이 클래스는 `Iterator` 인터페이스의 `rewind()` 메서드를 오버라이딩하여 rewind() 메서드를 호출할 때, 내부 포인터를 처음 위치로 이동시키지 않습니다.
`NoRewindIterator` 클래스는 `Iterator` 인터페이스에 구현된 `current()` 메서드를 오버라이딩하여, 내부 포인터의 현재 위치에 있는 값을 반환합니다.
NoRewindIterator::current 메서드
`NoRewindIterator::current()` 메서드는 내부 포인터의 현재 위치에 있는 값을 반환합니다. 이 메서드는 `rewind()` 메서드를 호출하지 않습니다.
예제
#hostingforum.kr
php
class MyIterator implements Iterator {
private $data;
private $position;
public function __construct(array $data) {
$this->data = $data;
$this->position = 0;
}
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->data[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
$this->position++;
}
public function valid() {
return $this->position < count($this->data);
}
}
$data = [1, 2, 3, 4, 5];
$iterator = new MyIterator($data);
$noRewindIterator = new NoRewindIterator($iterator);
echo $noRewindIterator->current(); // 1
$noRewindIterator->next();
echo $noRewindIterator->current(); // 1
위 예제에서, `MyIterator` 클래스는 `Iterator` 인터페이스를 구현한 클래스입니다. `NoRewindIterator` 클래스는 `MyIterator` 클래스를 생성자에 전달하여 내부 포인터를 사용합니다.
`NoRewindIterator` 클래스의 `current()` 메서드는 내부 포인터의 현재 위치에 있는 값을 반환합니다. 그러나 `rewind()` 메서드를 호출하지 않습니다.
`MyIterator` 클래스의 `rewind()` 메서드는 내부 포인터를 처음 위치로 이동시킵니다. 그러나 `NoRewindIterator` 클래스의 `current()` 메서드는 내부 포인터의 현재 위치에 있는 값을 반환합니다.
위 예제에서, `NoRewindIterator` 클래스의 `current()` 메서드는 내부 포인터의 현재 위치에 있는 값을 반환합니다. 그러나 `rewind()` 메서드를 호출하지 않습니다.
결론
`NoRewindIterator::current` 메서드는 내부 포인터의 현재 위치에 있는 값을 반환합니다. 이 메서드는 `rewind()` 메서드를 호출하지 않습니다. `NoRewindIterator` 클래스는 `Iterator` 인터페이스를 구현한 클래스입니다. 이 클래스는 `Iterator` 인터페이스의 `rewind()` 메서드를 오버라이딩하여 rewind() 메서드를 호출할 때, 내부 포인터를 처음 위치로 이동시키지 않습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.