| mx.utils 包 [1] 之Collection&Iterator |
|
mx.utils 包 [1] 之Collection&Iterator |
| 作者:佚名 来源:闪吧 作者: LeeFJ 更新:2007-1-13 20:47:15 错误报告 我要投稿 |
在 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,这个时候我大概已经了解是什么意思了吧,我想。呵呵,也许还没有。哈哈
|
|
| 文章录入:skyuu 责任编辑:skyuu |
|
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |