迭代器模式:将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构*/

<!-- 迭代器:是遍历集合的成熟模式-->
<?php
/*
迭代器模式:将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对象,而客户端程序员不必知道或关心该集合序列底层的结构*/
interface Iterators{
public function First();
public function Pre();
public function Next();
public function End();
public function CurrentItem();
}
?>


<?php
require_once "Iterators.php";
class Iteratorobj implements Iterators{
public $objects;
public $current = 0;
public function __construct($objects)
{
$this->objects = $objects;
}


public function First()
{
return $this->objects[0];
}


public function Pre()
{
$this->current--;
if($this->current < 0 ){
return false;
}else{
return $this->objects[$this->current];
}
}


public function Next()
{
$this->current++;
if($this->current <= count($this->objects))
{
return $this->objects[$this->current];
}
return false;
}


public function End()
{
return $this->objects[count($this->objects)-1];
}




public function CurrentItem()
{
return $this->objects[$this->current];
}
}
?>




<?php
require_once "Iteratorobj.php";
$arr = array('hello',"world","say");
$obj = new Iteratorobj($arr);
echo $obj->First()."<br>";
echo $obj->End()."<br>";
echo $obj->Next()."<br>";
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值