博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php-Iterators迭代器
阅读量:7113 次
发布时间:2019-06-28

本文共 2064 字,大约阅读时间需要 6 分钟。

一个对象的属性(也不能说是遍历对象的属性,遍历什么,还得看current函数里面写的是什么。)可以用foreach()循环进行迭代遍历:

任何实现Traversable接口的类都可以用foreach结构遍历。但是,Traversable是一个空的接口而且不能被直接执行;反之,你可以执行Iterator或者IteratorAggregate,它们都是从Traversable继承而来。

主要的接口是Iterator,接口的主要方法如下:

void rewind() 重新把迭代器指向列表的开始处(这个在执行时并不总是可用的)
mixed current() 返回当前位置的值
mixed key()    返回当前位置的关键字
void next() 把迭代器移动到下一个关键字/值对
bool valid() 返回true/false值,判断是否还有更多的值(在调用current()和key()之前使用)
start=$start; $this->end=$end; } public function rewind(){ $this->cur=$this->start; } public function key(){ return $this->cur; } public function current(){ return pow($this->cur,2); } public function next(){ $this->cur++; } public function valid(){ return $this->cur <= $this->end; } private $start,$end; private $cur; } $obj=new NumberSquared(3,7); foreach($obj as $key => $value){ print "The square of $key is $value
"; }?>

目前在许多情况下,类本身将表示数据和拥有与这些数据交互的方法。事实上,需要一个迭代器可能不是它的主要目的。另外,当迭代遍历一个对象的时候,迭代的状态(当前的位置)通常会存储在对象本身,因此不允许迭代的嵌套。所以,可以让类实现IteratorAggregate接口从而把类的执行和它的迭代其分离开来。

下面是例子代码:

start=$start; $this->end=$end; } public function getIterator(){ return new NumberSquaredIterator($this); } public function getStart(){ return $this->start; } public function getEnd(){ return $this->end; } private $start,$end;}class NumberSquaredIterator implements Iterator{ function __construct($obj){ $this->obj=$obj; } public function rewind(){ $this->cur=$this->obj->getStart(); } public function key(){ return $this->cur; } public function current(){ return pow($this->cur,2); } public function next(){ $this->cur++; } public function valid(){ return $this->cur <= $this->obj->getEnd(); } private $obj; private $cur;} $obj=new NumberSquared(3,7); foreach($obj as $key => $value){ print "The square of $key is $value
"; }?>

两个例子的执行方法都一样,IteratorAggregate接口可以让类的主要功能与迭代遍历需要的方法分离到两个独立的实体中。

为什么这样做,现在我还不是特别能体会和理解。

转载于:https://www.cnblogs.com/nana135/p/6433682.html

你可能感兴趣的文章
不老的神器:安全扫描器Nmap渗透使用指南【转】
查看>>
Java-NIO(六):Channel聚集(gather)写入与分散(scatter)读取
查看>>
CUBA如何新增ServiceBean
查看>>
【开源分享:入门到精通ASP.NET MVC+EF6+Bootstrap】从这里开始,一起搭框架(1)开篇介绍...
查看>>
【技术文档】jeecg3.7-maven搭建好开发环境入门
查看>>
centos7 关闭firewall安装iptables并配置
查看>>
搜索7--noi1804:小游戏
查看>>
聊一聊分布式锁的设计
查看>>
模运算的规则
查看>>
Nginx + Tomcat 动静分离实现负载均衡
查看>>
浏览器配置工具.bat
查看>>
ViewPager实现引导页
查看>>
Image Filter
查看>>
项目笔记:新增、编辑与删除
查看>>
前向星和链式前向星
查看>>
3.1软件体系结构风格
查看>>
LinkedHashMap 源码解析
查看>>
MySQL--当事务遇到DDL命令
查看>>
Linux系统centOS7在虚拟机下的安装及XShell软件的配置
查看>>
网络知识 ACL NAT IPv6
查看>>