ActionScript2.0:CollectionとIterator

オブジェクト脳のつくり方」に出ていたCollectionとIteratorを使ったJavaのサンプルをAS2.0で試してみました。ググッったらLiveDocにIteratorインターフェースの解説があった。Collectionのサンプルが見つからなかったので mx.utils の中のクラスを探したら IteratorImpl.asとCollectionImpl.asを発見。Iteratorの方はJavaと同じ使い方っぽいけど、Collectionの方はメソッド名が違うので載せときます。

  • addItem(item:Object):Boolean
  • clear():Void
  • contains(item:Object):Boolean
  • isEmpty():Boolean
  • getIterator():Iterator
  • getLength():Number
  • getItemAt( index:Number ):Object
  • removeItem(item:Object):Boolean

オブジェクト脳のつくり方」に出てくるサンプルをAS2に書き直したもの。実際には他に4つぐらいクラスがあるのでこれだけでは動かないけど参考になれば。リクエストがあればまとめてzipファイルアップします。

import mx.utils.CollectionImpl;
import mx.utils.Collection;
import mx.utils.IteratorImpl;
import mx.utils.Iterator;
class Bumon implements Party{
	public function Bumon(name:String){
		this.name = name;
		parties = new CollectionImpl();
	}
	public function addParty(party:Party):Void {
		parties.addItem(party);
	}
	public function hyoji():Void{
		trace("\n" + name);
		trace("----------------------------");
		var itr:Iterator = parties.getIterator();
		while(itr.hasNext()){
			var party:Party = Party(itr.next());
			party.hyoji();
		}
	}
	private var name:String;
	private var parties:Collection;
}

***オブジェクト指向参考書***

This entry was posted in FLASH Actionscript. Bookmark the permalink.

One Response to ActionScript2.0:CollectionとIterator

  1. じびき says:

    Iterator、まだほとんど理解していません。
    昔のオブジェクト思考言語ではほとんど使われていませんでした。未だにC++が他の言語に負けないように無理矢理作った、という感じが抜けないのです。(^^;

コメントをどうぞ

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>