设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

mx.utils 包 [1] 之Collection&Iterator

来源:136z.Com 作者:佚名 时间:2005-06-22 00:03:38



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接口是如何使用的,这里看看老外(不好意思,忘记从哪个站点下的了)的例子代码吧:

//导入这个包
importmx.utils.*;
//创建一个新的Collection,CollectionImpl是Collection接口的实现,等一下看看它的代码
varmyColl:Collection=newCollectionImpl();
//添加一个字符串到Collection
myColl.addItem("foobar");
//添加一个字数组到Collection
myColl.addItem([1,2,3]);
//添加一个日期对象到Collection
myColl.addItem(newDate());
//得到一个Iterator并且循环打印
varmyIterator:Iterator=myColl.getIterator();

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

importmx.utils.Iterator;

interfacemx.utils.Collection
{
publicfunctionaddItem(item:Object):Boolean;
publicfunctionclear():Void;
publicfunctioncontains(item:Object):Boolean;
publicfunctiongetItemAt(index:Number):Object;
publicfunctiongetIterator():mx.utils.Iterator;
publicfunctiongetLength():Number;
publicfunctionisEmpty():Boolean;
publicfunctionremoveItem(item:Object):Boolean;
};
然后是他的实现代码,我用硕思2005搞到的,呵呵,比较长
classmx.utils.CollectionImplextendsObjectimplementsmx.utils.Collection
{
//在这里先定义一个数组,是作为存储引用的吧
var_items;
//构造函数
functionCollectionImpl()
{
super();
_items=newArray();
}//Endofthefunction
functionaddItem(item)
{
var_l2=false;
if(item!=null)
{
this._items.push(item);
_l2=true;
}//endif
return(_l2);
}//Endofthefunction
functionclear()
{
_items=newArray();
}//Endofthefunction
functioncontains(item)
{
return(this.internalGetItem(item)>-1);
}//Endofthefunction
functiongetItemAt(index)
{
return(this._items[index]);
}//Endofthefunction
//我觉得其他不用说了,关键在这里!!
//那么我们看看这个IteratorImpl,它是Iterator的实现类
functiongetIterator()
{
return(newmx.utils.IteratorImpl(this));
}//Endofthefunction
functiongetLength()
{
return(this._items.length);
}//Endofthefunction
functionisEmpty()
{
return(this._items.length==0);
}//Endofthefunction
functionremoveItem(item)
{
var_l2=false;
var_l3=this.internalGetItem(item);
if(_l3>-1)
{
this._items.splice(_l3,1);
_l2=true;
}//endif
return(_l2);
}//Endofthefunction
functioninternalGetItem(item)
{
var_l3=-1;
var_l2=0;
while(_l2{
if(this._items[_l2]==item)
{
_l3=_l2;
break;
}//endif
_l2++;
}//endwhile
return(_l3);
}//Endofthefunction
}//EndofClass

这是Iterator接口,很简单,只有两个方法
interfacemx.utils.Iterator
{
publicfunctionhasNext():Boolean;
publicfunctionnext():Object;
};
然后看看这个接口的实现类:
classmx.utils.IteratorImplimplementsmx.utils.Iterator
{
var_collection,_cursor;
functionIteratorImpl(coll)
{
_collection=coll;
_cursor=0;
}//Endofthefunction
functionhasNext()
{
return(this._collection.getLength()>this._cursor);
}//Endofthefunction
functionnext()
{
_cursor=this._cursor++;
return(this._collection.getItemAt(this._cursor));
}//Endofthefunction
}//EndofClass 不多说了,也很简单。AOL说要做个Framework,这个时候我大概已经了解是什么意思了吧,我想。呵呵,也许还没有。哈哈

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: