简体   繁体   English

在Actionscript 3中扩展数组(Flex)

[英]Extending Array in Actionscript 3 (Flex)

I'm trying to make a variation on Array for a very specific purpose. 我正在尝试为Array做出一个非常特殊的变化。 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: 我收到的错误是这样的:

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

Instead of extending Array you could write your own class that exposes all the methods of Array. 您可以编写自己的类来公开Array的所有方法,而不是扩展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: 通过使用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];
        }

    }
}

example: 例:

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 

[] only creates an Array. []仅创建一个数组。 It cannot be used to create a subclass of Array. 它不能用于创建Array的子​​类。

The good way to "extend" Array with new functionality is to write standalone utility functions that manipulate regular Arrays. 使用新功能“扩展”Array的好方法是编写操作常规数组的独立实用程序函数。 Best of all, this will allow you to do anything to any Array and not be limited only to Arrays created using your subclass. 最重要的是,这将允许您对任何数组执行任何操作,而不仅限于使用您的子类创建的数组。

Here's a simple example of a class that contains utility functions for Arrays: 这是一个包含Arrays实用函数的类的简单示例:

package com.example.utils
{
    public class ArrayUtil
    {
        public static function traceArray( array:Array ):void
        {
            trace( array.length, "[" + array + "]" );
        }
    }
}

Usage: 用法:

ArrayUtil.traceArray( [1, 2, 3] ); //output: 3 [1,2,3]

[1,2,3] is shorthand (or syntactic sugar) for new Array(1,2,3) . [1,2,3]new Array(1,2,3)简写(或语法糖new Array(1,2,3) With that in mind, it seems more apparent why your code fails. 考虑到这一点,您的代码失败似乎更为明显。

Every TileArray is an Array , since TileArray extends Array , but the inverse is not true: not every Array is a TileArray . 每个TileArray都是一个Array ,因为TileArray扩展了Array ,但反之则不然:并非每个Array都是TileArray So, you can't pass an Array where a TileArray is expected. 因此,您无法传递期望TileArrayArray That's why you get the compiler error. 这就是你得到编译器错误的原因。

Casting will only defer the error from compile-time to run-time, since the actual type of your object is Array , which is indeed unrelated to TileArray . 转换只会将错误从编译时推迟到运行时,因为对象的实际类型是Array ,这实际上与TileArray无关。

If you want to extend Array functionality (and also be able to add some syntactic sugar), you might want to look into extending Proxy , as it was already suggested. 如果您想扩展Array功能(并且还能够添加一些语法糖),您可能需要查看扩展Proxy ,因为它已经建议。 Keep in mind it's less performant, so if you plan to use this class heavily, this might not be the best idea. 请记住,它的性能较差,所以如果你计划大量使用这个类,这可能不是最好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM