//导入这个包
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,这个时候我大概已经了解是什么意思了吧,我想。呵呵,也许还没有。哈哈
共有 0 位网友发表了评论,得分 0 分,平均 0 分 查看完整评论