MX.UTILS package Collection & Iterator
In the mx.utils package, including a lot of classes, you can find the Delegate class directly in the installation directory, other include Collection interface, CollectionImpl class, ERRORSTRINGS class, Iterator interface, IteratorImpl class, ObjectCopy class, StringTokenParser class, Xmlstring class. In fact, certain things are still relatively small. I read some information on the Internet today understood it. Of course, I also teach a few questions to the AOL brother, I feel that he is a master of Flash, admire.
Let's make a small explore on the Collection interface, the Iterator interface, huh, huh. Use the Collection interface to first import CLASSES in Common Libraies. Then drag the utilsclasses in the library panel, you can. Of course, I estimate that other classes or interfaces in the MX.UTILS package are also doing this, but I want to know now, what is the utilsclasses in the public library of the Classes, what should I do? Oh, it's okay to use some ASV tools. It is necessary to see how the Collection interface is used here. Here is the example code of the foreigners (sorry, forget which site):
// Import this package
Import mx.utils. *;
// Create a new Collection, CollectionImpl is the implementation of the Collection interface, wait a look at its code
Var mycoll: Collection = New CollectionImpl ();
// Add a string to Collection
MyColl.Additem ("foo bar");
// Add a word array to Collection
MyColl.Additem ([1, 2, 3]);
// Add a date object to Collection
MyColl.Additem (new date ());
// Get an Iterator and recycle
Var myiterator: itrator = mycoll.getiterator ();
While (myiterator.hasnext ())
{
TRACE (MyITerator.next ());
}
In this way, it is easy to see what COLLECTION is doing. Just gather different objects together, simply like Java, if you understand the use of the Collection interface, it is necessary to go deep into it. Here, it can actually involve two interfaces and two classes. Collection interface, Iterator interface, CollectionImpl class, and IteratorIMPL class. Let's take a look at the Collection interface:
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.IpectR.
Public function getLength (): Number;
Public function iempty (): boolean; public function removeitem (item: object): boolean
}
Then it is his realization code, I have used it in STW 2005, huh, it is more
Class MX.UTILS.COLLECTIONIMPL EXTENDS Object IMPLEments MX.UTILS.COLLECTION
{
/ / First, define an array first, it is a storage reference.
Var_items;
//Constructor
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
/ / I don't want to say anything else, the key is here! !
// then let's take a look at this IteratorImpl, it is the implementation class of Iterator
Function Getiterator ()
{
Return (New MX.UTILS.ITERATORMPL (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 { IF (this._items [_l2] == iTEM) { _L3 = _L2; Break; } // end if _L2 ; } // End while Return (_L3); } // end of the function } // End of class This is the Iterator interface, very simple, only two ways Interface mx.utils.Istrator { Public function hasnext (): boolean; Public function next (): Object; } Then look at the implementation class of this interface: Class mx.utils.ITeratorImpl imports mx.utils.Istorator { 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 Not much to say, it is also very simple. AOL says to be a framework, this time I have probably understand what it means, I think. Oh, maybe it's okay. Haha