在ActionScript 3的扩展阵列(Flex的) [英] Extending Array in Actionscript 3 (Flex)

查看:226
本文介绍了在ActionScript 3的扩展阵列(Flex的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让阵的变化的一个非常具体的目标。当我有以下内容:

I'm trying to make a variation on Array for a very specific purpose. When I have the following:

public class TileArray extends Array {
   // Intentionally empty - I get the error regardless
}

我为什么不能这样做呢?

Why can't I do this?

var tl:TileArray = [1,2,3];

尽管我可以做到这一点。

despite the fact that I can do this

var ar:Array = [1,2,3];

我收到的错误是这样的:

The error I receive is this:

静态类型数组中的值到可能不相关类型的隐式强制

推荐答案

而是延伸数组,你可以编写自己的类,它暴露了数组中所有的方法。通过采用代理类,你可以重定向所有的默认阵列方法的内部数组,但仍然可以灵活地添加自己的方法:

Instead of extending Array you could write your own class that exposes all the methods of Array. By employing the Proxy class you can redirect all default Array methods to an internal array but still have the flexibility to add your own methods:

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];
        }

    }
}

例如:

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 

这篇关于在ActionScript 3的扩展阵列(Flex的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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