ActionScript 3 㧠Vector ã¯ã©ã¹ã®é åãåçã«çæããã«ã¯
ãªãã¸ã§ã¯ãã®ã·ãªã¢ã©ã¤ãºã«é¢ããå®è£ ãããä¸ã§ãVector ã¯ã©ã¹ã®ãªãã¬ã¯ã·ã§ã³ã¯ã§ããã®ã調ã¹ã¦ã¿ãã
Vectorã¯ã©ã¹åãåå¾
var classInfo:Object = ObjectUtil.getClassInfo(Vector.<Entity>); trace(classInfo.name);
ã¨ããã¨åºåã¯ä¸è¨ã®ããã«ãªã£ãã
__AS3__.vec::Vector.<com.tilfin.sample::Entity>
ããã©ã¯ãgetDefinitionByName ã使ã£ã¦çæãã¦ã¿ãã¨
var cls:Class = getDefinitionByName("__AS3__.vec::Vector.<com.tilfin.sample::Entity>") as Class var vector:Vector.<Entity> = new cls();
ãã¡ããã¨ä¸æããã£ããã¨ãããã¨ã§è²ã ã§ãããã§ãã
Vectorã¦ã¼ãã£ãªãã£ã¯ã©ã¹
ãµã³ãã«ã¨ãã¦è©¦ãã«æ¸ãã¦ã¿ãã
package { import flash.utils.getDefinitionByName; import flash.utils.getQualifiedClassName; public class VectorUtil { /** * æå®ããããªãã¸ã§ã¯ããã¨ã¬ã¡ã³ãã¨ããVectorã¯ã©ã¹ã®é åãè¿ãã¾ãã * * @param args ï¼ã¤ä»¥ä¸ã®ãªãã¸ã§ã¯ããããã¯é å * @return Vectorã¤ã³ã¹ã¿ã³ã¹ * */ public static function toVector(...args):* { var item:*; var items:Array; if (args.length == 1) { item = args[0]; if (item is Array) { items = item as Array; } else { items = [ item ]; } } else { items = args as Array; } if (!items || items.length == 0) { throw new ArgumentError("requires at least one argument."); } item = items.shift(); var vectorClass:Class = getVectorClass(item); var vector:* = new vectorClass(); vector.push(item); for each (var i:* in items) { vector.push(i); } return vector; } /** * æå®ããããªãã¸ã§ã¯ãã¯ã©ã¹ãæ ¼ç´ããVectorã¯ã©ã¹ãªãã¸ã§ã¯ããè¿ãã¾ãã * * @param value å®å ¨ä¿®é£¾ã¯ã©ã¹åãå¿ è¦ãªãªãã¸ã§ã¯ã * @return Vectorã¯ã©ã¹ãªãã¸ã§ã¯ã * */ public static function getVectorClass(value:*):Class { var className:String; if (value is String) { className = value; } else { className = getQualifiedClassName(value); } return getDefinitionByName("__AS3__.vec::Vector.<" + className + ">") as Class; } } }
ä¸è¨ã®ã¨ã³ãã£ãã£ã¯ã©ã¹ã使ã£ã¦ä¾ãå®è¡ãã¦ã¿ãã
package com.tilfin.sample { public class Entity { private var _name:String; public function Entity(name:String) { _name = name; } public function get name():String { return _name; } } }
import com.tilfin.sample.Entity; private function sample():void { var et1:Entity = new Entity("sample1"); var et2:Entity = new Entity("sample2"); var vector:Vector.<Entity> = VectorUtil.toVector(et1, et2); for each (var item:Entity in vector) { trace(item.name); } }
ãããªãããã«æ¸ãã¨ãä¸è¨ã®ããã«åºåãããã
sample1 sample2
ãªããVector ã¯ã©ã¹ã¯ Flash Player 10 ããã㯠AIR 1.5 以éãå¿ é ã§ãã