需求
设计一个集合,可以遍历机能,为坦克装配机能和遍历运行机能。
运行
代码
console.log('迭代器演示:');
class Function {
constructor(fun) {
this.fun = fun;
}
exe(guige) {
console.log(this.fun + guige);
}
}
class Item {
constructor(data) {
this.data = data;
//this.qian = null;
this.hou = null;
}
}
class Aggregate {
constructor() {
this.index = 0;
this.firstItem = null;
this.curentItem = null;
this.lastItem = null;
}
add(fun) {
var item = new Item(fun);
if (this.lastItem == null) {
this.lastItem = item;
this.firstItem = item;
} else {
//item.qian = this.lastItem;
this.lastItem.hou = item;
this.lastItem = item;
}
}
initCurentItem() {
this.curentItem = this.firstItem;
}
getNext() {
if (this.curentItem == null) {
return null;
}
let rd = this.curentItem.data;
this.curentItem = this.curentItem.hou;
return rd;
}
}
class Tank {
constructor(guige) {
this.guige = guige;
this.jiehe = new Aggregate();
}
exe() {
let flag = true;
this.jiehe.initCurentItem();
while (flag) {
let fun = this.jiehe.getNext();
if (fun != null) {
fun.exe(this.guige);
} else {
flag = false;
}
}
}
zhuangPeiFun(fun) {
this.jiehe.add(fun)
}
}
class B50Tank extends Tank {
constructor() {
super(50);
}
}
// 客户端
class Client {
main() {
//this.test1();
//this.test2();
let b50Tank = new B50Tank();
b50Tank.zhuangPeiFun(new Function("射击"));
b50Tank.zhuangPeiFun(new Function("瞄准"));
b50Tank.zhuangPeiFun(new Function("跑"));
b50Tank.exe();
}
test2() {
// 集合实验-对象
var aggregate = new Aggregate();
aggregate.add(new Function("射击"));
aggregate.add(new Function("瞄准"));
aggregate.add(new Function("跑"));
aggregate.initCurentItem();
var flag = true;
while (flag) {
var a = aggregate.getNext();
if (a == null) {
flag = false;
} else {
console.log(a);
}
}
}
test1() {
// 集合实验-简单数据
/*
var aggregate = new Aggregate();
aggregate.add(1);
aggregate.add(2);
aggregate.add(3);
aggregate.initCurentItem();
var flag = true;
while (flag) {
var a = aggregate.getNext();
if (a == null) {
flag = false;
} else {
console.log(a);
}
}
*/
}
}
var client = new Client();
client.main();
第二版代码(通常迭代器和集合是分开的,但是现在通常的用法都已经把机能都集合的集合里了,索引这里也做过简单的切分)
console.log('迭代器演示:');
class Function {
constructor(fun) {
this.fun = fun;
}
exe(guige) {
console.log(this.fun + guige);
}
}
class Item {
constructor(data) {
this.data = data;
//this.qian = null;
this.hou = null;
}
}
class Aggregate {
constructor() {
this.iterator = new Iterator();
}
add(fun) {
this.iterator.add(fun);
}
initCurentItem() {
this.iterator.initCurentItem();
}
getNext() {
return this.iterator.getNext();
}
}
class Iterator {
constructor() {
this.index = 0;
this.firstItem = null;
this.curentItem = null;
this.lastItem = null;
}
add(fun) {
var item = new Item(fun);
if (this.lastItem == null) {
this.lastItem = item;
this.firstItem = item;
} else {
//item.qian = this.lastItem;
this.lastItem.hou = item;
this.lastItem = item;
}
}
initCurentItem() {
this.curentItem = this.firstItem;
}
getNext() {
if (this.curentItem == null) {
return null;
}
let rd = this.curentItem.data;
this.curentItem = this.curentItem.hou;
return rd;
}
}
class Tank {
constructor(guige) {
this.guige = guige;
this.jiehe = new Aggregate();
}
exe() {
let flag = true;
this.jiehe.initCurentItem();
while (flag) {
let fun = this.jiehe.getNext();
if (fun != null) {
fun.exe(this.guige);
} else {
flag = false;
}
}
}
zhuangPeiFun(fun) {
this.jiehe.add(fun)
}
}
class B50Tank extends Tank {
constructor() {
super(50);
}
}
// 客户端
class Client {
main() {
//this.test1();
//this.test2();
let b50Tank = new B50Tank();
b50Tank.zhuangPeiFun(new Function("射击"));
b50Tank.zhuangPeiFun(new Function("瞄准"));
b50Tank.zhuangPeiFun(new Function("跑"));
b50Tank.exe();
}
test2() {
// 集合实验-对象
var aggregate = new Aggregate();
aggregate.add(new Function("射击"));
aggregate.add(new Function("瞄准"));
aggregate.add(new Function("跑"));
aggregate.initCurentItem();
var flag = true;
while (flag) {
var a = aggregate.getNext();
if (a == null) {
flag = false;
} else {
console.log(a);
}
}
}
test1() {
// 集合实验-简单数据
/*
var aggregate = new Aggregate();
aggregate.add(1);
aggregate.add(2);
aggregate.add(3);
aggregate.initCurentItem();
var flag = true;
while (flag) {
var a = aggregate.getNext();
if (a == null) {
flag = false;
} else {
console.log(a);
}
}
*/
}
}
var client = new Client();
client.main();
英语
aggregate
vi. 集合; 聚集; 合计; vt. 集合; 聚集; 合计; n. 合计; 集合体; 总计; ad...
iterator
n. 迭代器; 迭代程序
concrete
adj. 混凝土的; 实在的,具体的; 有形的; vi. 凝结; vt. 使凝固; 用混凝土修筑; ...