Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3遍历类变量_Actionscript 3 - Fatal编程技术网

Actionscript 3 AS3遍历类变量

Actionscript 3 AS3遍历类变量,actionscript-3,Actionscript 3,如何遍历类实例的所有变量? 它似乎适用于常规对象 已编辑:这将完成它=) 输出: 对象字符串 111 2012年11月8日星期四11:19:27 GMT-0700 类字符串 222 2012年11月8日星期四11:19:27 GMT-0700 public class anyClass { public var val1:String="class string"; public var val2:int=222; public var val3:Date=new Dat

如何遍历类实例的所有变量? 它似乎适用于常规对象

已编辑:这将完成它=)

输出: 对象字符串 111 2012年11月8日星期四11:19:27 GMT-0700 类字符串 222 2012年11月8日星期四11:19:27 GMT-0700

public class anyClass
{
    public var val1:String="class string";
    public var val2:int=222;
    public var val3:Date=new Date();
}

我认为最简单的方法是使用
flash.utils.describeType()

()

这将返回一个描述类的所有细节的XML文档,然后可以使用普通E4X攻击该类。下面是我测试过的一个示例:

import flash.display.Sprite;
import flash.utils.describeType;

var test:String = "TEST";

function DescribeTypeExample():void {
    var child:Sprite = new Sprite();
    var description:XML = describeType(this);
    var variables:XMLList = description..variable;
    for each(var variable:XML in variables) {
        trace("VARIABLE: " + variable.@name);
        trace("VALUE: " + this[variable.@name]);
    }
}

this.DescribeTypeExample();

// Output:

// VARIABLE: test
// VALUE: TEST

看看对象类中的这些属性,哦,很好,我已经看到了,但是我想,“也许这个inst真的很有必要”。原来我错了。。。我在一个可绑定的ArrayCollection的项目上使用它,所以accessor为我做了。我意识到一个普通的Sprite没有任何变量(D'oh!),所以改为另一个例子,我已经测试过了。如果访问器对您有效,那就太好了。嘿,伙计们,我无法检索非动态类的变量。有什么方法可以做到这一点吗?@colling\uu如果您发布一个问题而不是一条评论,您更有可能得到答案。用一些示例代码展示您正在尝试做什么,以及为什么它不起作用——这样肯定会有人能够提供帮助。
import flash.display.Sprite;
import flash.utils.describeType;

var test:String = "TEST";

function DescribeTypeExample():void {
    var child:Sprite = new Sprite();
    var description:XML = describeType(this);
    var variables:XMLList = description..variable;
    for each(var variable:XML in variables) {
        trace("VARIABLE: " + variable.@name);
        trace("VALUE: " + this[variable.@name]);
    }
}

this.DescribeTypeExample();

// Output:

// VARIABLE: test
// VALUE: TEST