王朝网络
分享
 
 
 

mx.utils 包 之Collection&Iterator

王朝other·作者佚名  2006-01-29
宽屏版  字体: |||超大  

mx.utils 包 之Collection&Iterator

在 mx.utils包中,包括了不少类,直接可以在安装目录下找到的是Delegate类,其他的还包括Collection接口,CollectionImpl类,ErrorStrings类,Iterator接口,IteratorImpl类,ObjectCopy类,StringTokenParser类,XMLString类。其实某些东西还是用的比较少的。今天看了网上的一些资料自己理解整理了一下,当然也向AOL老兄请教了几个问题,感觉他真是个Flash方面的高手,佩服佩服。

下面先对Collection接口,Iterator接口做一个小小的探索,呵呵。使用Collection接口必须首先导入 Common Libraies中的Classes 。然后在库面板中把UtilsClasses拖近来,就可以了。当然,我估计使用mx.utils包中的其他类或者接口也是必须要这么做的,但是我现在想知道,那个Classes 的公共库中的UtilsClasses 到底有哪些东西,要怎么办呢?呵呵反正用一些ASV的工具就可以了。这里有必要先来看看Collection接口是如何使用的,这里看看老外(不好意思,忘记从哪个站点下的了)的例子代码吧:

//导入这个包

import mx.utils.*;

// 创建一个新的Collection,CollectionImpl是Collection接口的实现,等一下看看它的代码

var myColl:Collection = new CollectionImpl ();

// 添加一个字符串到Collection

myColl.addItem ("foo bar");

//添加一个字数组到Collection

myColl.addItem ([1, 2, 3]);

//添加一个日期对象到Collection

myColl.addItem (new Date ());

// 得到一个 Iterator 并且循环打印

var myIterator:Iterator = myColl.getIterator ();

while (myIterator.hasNext ())

{

trace (myIterator.next ());

}

这样,很容易看出Collection在做什么事情了。就是把不同的对象集合到一起,简直太像java了,如果了解了Collection接口的使用以后,那就很有必要往深入看看了。这里其实可以涉及到两个接口和两个类。Collection接口、Iterator接口、CollectionImpl类和Iteratorimpl类。先来看看Collection接口:

import mx.utils.Iterator;

interface mx.utils.Collection

{

public function addItem(item:Object):Boolean;

public function clear():Void;

public function contains(item:Object):Boolean;

public function getItemAt(index:Number):Object;

public function getIterator():mx.utils.Iterator;

public function getLength():Number;

public function isEmpty():Boolean;

public function removeItem(item:Object):Boolean;

};

然后是他的实现代码,我用硕思2005搞到的,呵呵,比较长

class mx.utils.CollectionImpl extends Object implements mx.utils.Collection

{

//在这里先定义一个数组,是作为存储引用的吧

var _items;

//构造函数

function CollectionImpl()

{

super();

_items = new Array();

} // End of the function

function addItem(item)

{

var _l2 = false;

if (item != null)

{

this._items.push(item);

_l2 = true;

} // end if

return(_l2);

} // End of the function

function clear()

{

_items = new Array();

} // End of the function

function contains(item)

{

return(this.internalGetItem(item) > -1);

} // End of the function

function getItemAt(index)

{

return(this._items[index]);

} // End of the function

//我觉得其他不用说了,关键在这里!!

//那么我们看看这个IteratorImpl,它是Iterator的实现类

function getIterator()

{

return(new mx.utils.IteratorImpl(this));

} // End of the function

function getLength()

{

return(this._items.length);

} // End of the function

function isEmpty()

{

return(this._items.length == 0);

} // End of the function

function removeItem(item)

{

var _l2 = false;

var _l3 = this.internalGetItem(item);

if (_l3 > -1)

{

this._items.splice(_l3, 1);

_l2 = true;

} // end if

return(_l2);

} // End of the function

function internalGetItem(item)

{

var _l3 = -1;

var _l2 = 0;

while (_l2 < this._items.length)

{

if (this._items[_l2] == item)

{

_l3 = _l2;

break;

} // end if

_l2++;

} // end while

return(_l3);

} // End of the function

} // End of Class

这是Iterator接口,很简单,只有两个方法

interface mx.utils.Iterator

{

public function hasNext():Boolean;

public function next():Object;

};

然后看看这个接口的实现类:

class mx.utils.IteratorImpl implements mx.utils.Iterator

{

var _collection, _cursor;

function IteratorImpl(coll)

{

_collection = coll;

_cursor = 0;

} // End of the function

function hasNext()

{

return(this._collection.getLength() > this._cursor);

} // End of the function

function next()

{

_cursor = this._cursor++;

return(this._collection.getItemAt(this._cursor));

} // End of the function

} // End of Class

不多说了,也很简单。AOL说要做个Framework,这个时候我大概已经了解是什么意思了吧,我想。呵呵,也许还没有。哈哈

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有