在 Actionscript 3 (Flex) 中扩展数组

发布于 2024-09-06 09:05:58 字数 375 浏览 6 评论 0原文

我正在尝试对 Array 进行变体以达到非常特定的目的。当我有以下问题时:

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

为什么我不能这样做?

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

尽管我可以做到这一点,但

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

我收到的错误是这样的:

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

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

氛圍 2024-09-13 09:05:58

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

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 

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

    }
}

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 
倾其所爱 2024-09-13 09:05:58

[] 只创建一个数组。它不能用于创建 Array 的子类。

使用新功能“扩展”数组的好方法是编写操作常规数组的独立实用程序函数。最重要的是,这将允许您对任何数组执行任何操作,而不仅限于使用您的子类创建的数组。

下面是一个包含数组实用函数的类的简单示例:

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

用法:

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

[] only creates an Array. It cannot be used to create a subclass of Array.

The good way to "extend" Array with new functionality is to write standalone utility functions that manipulate regular Arrays. 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:

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]
阳光的暖冬 2024-09-13 09:05:58

[1,2,3]new Array(1,2,3) 的简写(或语法糖)。考虑到这一点,代码失败的原因似乎就更明显了。

每个 TileArray 都是一个 Array,因为 TileArray 扩展了 Array,但反之则不然:不是每个 >Array 是一个TileArray。因此,您不能在需要 TileArray 的地方传递 Array。这就是您收到编译器错误的原因。

转换只会将错误从编译时推迟到运行时,因为对象的实际类型是 Array,这确实与 TileArray 无关。

如果您想扩展 Array 功能(并且还能够添加一些语法糖),您可能需要考虑扩展 Proxy,正如已经建议的那样。请记住,它的性能较差,因此如果您打算大量使用此类,这可能不是最好的主意。

[1,2,3] is shorthand (or syntactic sugar) for 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. So, you can't pass an Array where a TileArray is expected. 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.

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. Keep in mind it's less performant, so if you plan to use this class heavily, this might not be the best idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文