在Actionscript 3中扩展数组(Flex)

Jam*_*ong 8 apache-flex arrays extends actionscript-3 mxmlc

我试图在Array上做一个非常特殊的变化.当我有以下内容时:

public class TileArray extends Array {
   // Intentionally empty - I get the error regardless
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样做?

var tl:TileArray = [1,2,3];
Run Code Online (Sandbox Code Playgroud)

尽管我能做到这一点

var ar:Array = [1,2,3];
Run Code Online (Sandbox Code Playgroud)

我收到的错误是这样的:

Implicit coercion of a value with static type Array to a possibly unrelated type

Qua*_*ndo 13

您可以编写自己的类来公开Array的所有方法,而不是扩展Array.通过使用Proxy类,您可以将所有默认Array方法重定向到内部数组,但仍可以灵活地添加自己的方法:

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

    use namespace flash_proxy;

    dynamic public class ExampleArray extends Proxy
    {
        private var _array:Array;

        public function ExampleArray(...parameters)
        {
            _array = parameters;
        }

        override flash_proxy function callProperty( name:*, ...rest):* 
        {
            return _array[name].apply(_array, rest);

        }

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

        override flash_proxy function setProperty(name:*, value:*):void 
        {
            _array[name] = value;
        }

        public function getSmallestElement():*
        {
            var helper:Array = _array.concat().sort();
            return helper[0];
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

例:

var test:ExampleArray = new ExampleArray(8,7,6,5,4,3,2,1);
trace( test.getSmallestElement()); // 1
test.sort();
trace(test); // 1,2,3,4,5,6,7,8 
Run Code Online (Sandbox Code Playgroud)