枚举类属性 [英] Enumerating class properties

查看:174
本文介绍了枚举类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个自定义类的属性,然而,<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html#setPropertyIsEnumerable%28%29"相对=nofollow>由Adobe提供的方法似乎不起作用。我没有收到编译或运行时错误。

I'm trying to iterate through the properties of a custom class, however, the methods provided by Adobe appear to not work. I'm not receiving compile or run-time errors.

package {   
    public dynamic class enum {
        public var foo:Number = 123;

        public function enum() {
            this.setPropertyIsEnumerable("foo", true);
            if (this.propertyIsEnumerable("foo") == false) {
                trace("foo:" + foo + " is not enumerable.")
            }
        }
    }
}

// outputs "foo:bar is not enumerable."

Implementaiton

var test:enum = new enum();
for (var property:String in test) {
    trace(property);
}

// outputs nothing

我尽量保持我的code快速和灵活,所以它真的令人沮丧,当你必须改变类动态只是为了能够使用的...在的属性。 杰克逊·邓斯坦的测试证实,这可能是比静态类属性 400X速度较慢,但​​这些都必须明确引用(类(计算昂贵)的不切实际的财产无关的方法),或使用反射来访问。

I try to keep my code fast and flexible, so it's really frustrating when you must change the class to Dynamic just to be able to use for ... in on the properties. Jackson Dunstan's testing confirms that this can be 400x slower than static class properties, but those have to be explicitly referenced (impractical for property agnostic methods), or use reflection of the class (computationally expensive) to be accessible.

我发现回避整个问题的唯一方法是动态使用声明的变量...这是因为在使用setPropertyIsEnumerable(道具,真)是多余的那点毫无意义;所有动态创建的属性的已经是的枚举。此外,动态变量不能强烈datatyped和性能熄灭的窗口。

The only way I've found to sidestep the whole issue is to use dynamically declared variables... which is pointless since at that point using setPropertyIsEnumerable(prop, true) is superfluous; all dynamically created properties already are enumerable. Additionally, dynamic variables cannot be strongly datatyped, and performance goes out the window.

例如...

package {   
    public dynamic class enum {
        public var foo:String = "apple";

        public function enum(){
            this.dynamicVar = "orange";
            this.dynamicProp = "banana";
            this.setPropertyIsEnumerable("foo", true);
            this.setPropertyIsEnumerable("dynamicProp", false);
        }
    }
}

执行

var test:enum = new enum();
for (var key:String in test) {
    trace(key + ": " + test[key]); // dynamicVar: 1
}

// outputs "dynamicVar: orange"

现在这个类是动态的,我们看到的只有我们的一个3测试属性正在被重复。应该有2

Now that the class is dynamic, we see that only one of our 3 test properties are being iterated. There should be 2.

它几乎感觉就像Adobe希望我们采取不好的编程习惯。话让我失望......

It almost feels like Adobe wants us to adopt bad programming habits. Words fail me...

推荐答案

非动态类不提供枚举的属性或方法。

Non-dynamic classes do not provide enumerable properties or methods.

正如在你所提供的链接的描述

As stated in the description of the link you provided.

<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html#setPropertyIsEnumerable%28%29"相对=nofollow>设置动态属性循环操作的可用性。

我想你可能要重构code这种方法。 我从未有过遍历一个类的属性像你在这里做什么。 如果你想跟踪项目动态,你应该使用关联数组和跟踪他们的方式不是在类级别喜欢你在做什么。
如果你想强数据类型,然后使用一个载体。

I think you might want to refactor your code on this approach. I have never had to loop over a classes properties like you are doing here. If you want to track items dynamically you should use an associative array and track them that way not at the class level like you are doing.
And if you want strong data typing then use a vector.

这篇关于枚举类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆