gpt4 book ai didi

apache-flex - 在 Actionscript 3 (Flex) 中扩展数组

转载 作者:行者123 更新时间:2023-12-03 12:33:51 26 4
gpt4 key购买 nike

我正在尝试为一个非常特定的目的对 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];

我收到的错误是这样的:
Implicit coercion of a value with static type Array to a possibly unrelated type

最佳答案

您可以编写自己的类来公开 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];
}

}
}

例子:
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

关于apache-flex - 在 Actionscript 3 (Flex) 中扩展数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com