<?php
header("Content-Type:text/html;charset=utf-8");
abstract class llIterator
{
// 第一个
public abstract function First();
// 最后一个
public abstract function End();
// 下一个
public abstract function Next();
// 前一个
public abstract function Pre();
// 当前
public abstract function Current();
// 指针返回
public abstract function back();
}
class Con extends llIterator
{
private $any;
private $current = 0;
public function __construct(array $anys)
{
$this->any = $anys;
}
public function First()
{
$this->current = 0;
return $this->any[0];
}
public function End()
{
$this->current = count($this->any) - 1;
return $this->any[count($this->any) - 1];
}
public function Next()
{
$this->current++;
return $this->any[$this->current];
}
public function Pre()
{
$this->current--;
return $this->any[$this->current > 5 ? 4 : $this->current];
}
public function Current()
{
return $this->any[$this->current > 5 ? 4 : $this->current];
}
public function back()
{
$this->current = 0;
}
}
$test = new Con(array('蓝猫','火猫','土猫','大熊猫','龙鹰'));
echo $test->First();
echo $test->Next();
echo $test->End();
echo $test->Current();
echo $test->Pre();
echo $test->back();
PHP 迭代器模式 一个简单的迭代器
最新推荐文章于 2023-04-04 09:09:17 发布