如何使一个用户自定义对象的Iterable在AS3

问题描述:

我需要遍历一个用户自定义对象的所有属性。如何使对象可迭代的AS3? (我想用在一个for ... each循环)。

I need to iterate through all the properties of an user defined object. How to make an Object Iterable in As3? (I want to use that in a for ... each loop).

我试图扩大代理。

但是,并非一定要实现这个什么方法。

But not sure what methods to implement for this.

是我的课,

[Bindable]
public class XYZ extends Proxy
{
    public var aggTerRedQty:String;

    public var aggNBPQtyUsrs:String;

    public var termAllocUnloadQty:String;

    public var effNomQty:String;

    public var termAllocSplitFirm:String;

}

感谢

我在code犯了一个错误,以支持每个循环,编辑要解决这个问题。

I made a mistake in the code to support a for each loop, editing to fix that.

C $ C>代理类,我刮起了这个例子。我以前从未做过,和实现似乎有点奇怪。但是,这code主要是从文档,和它的作品:您可以使用每个环或环路上延伸代理

Based on the documentation for the Proxy class, I whipped up this example. I've never done it before, and the implementation seems a bit odd. But this code is mostly from the documentation, and it works: you can use a for each loop or a for in loop on the class that extends Proxy.

主要类:

package 
{ 
    import flash.display.Sprite; 

    public class Main extends Sprite  
    { 
        public function Main():void 
        { 
            var obj:MyClass = new MyClass();

            trace("for in loop output:");
            for (var propertyName:String in obj)
            {
                trace("property: " + propertyName + "  value: " + obj[propertyName]);
            }

            trace("for each loop output:")
            for each (var item:Object in obj)
            {
                trace("current item: " + item);
            }
        } 
    } 
}

类扩展代理

package
{
    import flash.display.Sprite;
    import flash.utils.Proxy;
    import flash.utils.flash_proxy;

    use namespace flash_proxy;

    public class MyClass extends Proxy
    {
        protected var _target:Object = { property1: property1, property2: property2, property3: property3, property4: property4 };
        protected var _item:Array;

        public function MyClass()
        {
        }

        override flash_proxy function nextNameIndex (index:int):int { 
            // initial call
            if (index == 0)
            {
                _item = new Array();
                for (var x:* in _target)
                {
                    _item.push(x);
                }
            }
            if (index < _item.length)
            {
                return index + 1;
            }
            else
            {
                return 0;
            }
        }

        override flash_proxy function nextName(index:int):String
        {
            return _item[index - 1];
        }

        override flash_proxy function nextValue(index:int):*
        {
            return _target[ _item[index -1] ];
        }

        override flash_proxy function getProperty(name:*):*
        {
            return _target[name];
        }


        public var property1:String = "Hi";
        public var property2:Boolean = true;
        public var property3:Object = { a: 1, b: false };
        public var property4:Sprite;
    }
}

控制台输出:

Console Output:

for in loop output:
property: property2  value: true
property: property4  value: null
property: property1  value: Hi
property: property3  value: [object Object]
for each loop output:
current item: true
current item: null
current item: Hi
current item: null

在文档的进一步审查,一个现实的实施代理类很可能还需要落实代理 setProxy()的方法为好。

On further review of the documentation, a realistic implementation of the Proxy class would probably also need to implement the Proxy class' setProxy() method as well.