在 Actionscript 3 (Flex) 中扩展数组 [英] Extending Array in Actionscript 3 (Flex)

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

问题描述

我正在尝试为一个非常特定的目的对 Array 进行变体.当我有以下情况时:

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
}

为什么我不能这样做?

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

尽管我可以做到这一点

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

我收到的错误是这样的:

The error I receive is this:

将静态类型 Array 的值隐式强制转换为可能不相关的类型

推荐答案

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

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天全站免登陆