-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIterator.php
More file actions
executable file
·142 lines (103 loc) · 3.5 KB
/
Iterator.php
File metadata and controls
executable file
·142 lines (103 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* 定义一个接口,所有项目都是一个接口, 面向接口编程当然要定义接口
*/
interface IProject {
/* 增加项目 */
public function add($name, $num, $cost);
/* 从老板这得到就是项目信息 */
public function getProjectInfo();
/* 获得一个可遍历的对象 */
public function iterator();
}
/**
* 所有项目的信息类
*/
class Project implements IProject {
/* 定义一个项目列表 */
private $project_list = array();
/* 项目名称 */
private $name = '';
/* 项目成员数 */
private $num = 0;
/* 项目费用 */
private $cost = 0;
public function __construct($name, $num, $cost) {
$this->name = $name;
$this->num = $num;
$this->cost = $cost;
}
public function add($name, $num, $cost) {
$this->project_list = array_push($this->project_list, new Project($name, $num, $cost));
}
public function getProjectInfo() {
$info = '';
$info .= '项目名称是:'.$this->name;
$info .= '项目人数是:'.$this->num;
$info .= '项目费用是:'.$this->cost;
return $info;
}
/* 产生一个遍历对象 */
public function iterator() {
return new ProjectIterator($this->project_list);
}
}
/* 老板来指定项目信息 */
$project_list = array();
array_push($project_list, new Project('星球大战项目', 10, 100000));
array_push($project_list, new Project('扭转时空项目', 100, 1000000));
array_push($project_list, new Project('超人改造计划', 1000, 10000000));
/* 这有10个项目 */
for($i = 0; $i < 10; $i++) {
array_push($project_list, new Project('第'.$i.'个项目', $i *5, $i * 10000));
}
foreach($project_list as $val) {
var_dump($val->getProjectInfo());
}
/* 想了下,遍历,可以用迭代实现 */
/**
* 定义一个迭代接口
*/
interface IProjectIterator {
}
class ProjectIterator implements IProjectIterator {
private $project_list = array();
private $currentItem = 0;
public function __construct($project_list) {
$this->project_list = $project_list;
}
/* 判断是否有元素 */
public function hasNext() {
$ret = true;
if ($this->currentItem > count($this->project_list) || @$this->project_list[$this->currentItem] == null) {
$ret = false;
}
return $ret;
}
/* 取得下一个值 */
public function next() {
return $this->project_list[$this->currentItem++];
}
/* 删除一个对象,暂时没用到 */
/*
public function remove() {
}
*/
}
/* 老板来看项目了啊 */
var_dump('***************************1*******************************');
$project = array();
array_push($project, new Project('星球大战项目', 10, 100000));
array_push($project, new Project('扭转时空项目', 100, 1000000));
array_push($project, new Project('超人改造计划', 1000, 10000000));
/* 这有10个项目 */
for($i = 0; $i < 10; $i++) {
array_push($project, new Project('第'.$i.'个项目', $i *5, $i * 10000));
}
/* 遍历一下数组,把所有数据都取出来 */
$project_iterator = new ProjectIterator($project);
while($project_iterator->hasNext()){
$p = $project_iterator->next();
var_dump($p->getProjectInfo());
}
?>