ActionScript 中具有多个基本类型的强类型集合(Vector)?

发布于 2024-10-26 02:43:33 字数 238 浏览 3 评论 0原文

ActionScript 是否有任何方法处理具有多个基本类型的强类型列表?

我实际上正在寻找诸如 Vector 之类的东西?

是否可以?

或者唯一的方法是创建我自己的类,该类在构造函数中接受 StringNumber 并创建一个 Vector代码> 那个类之外?

Does ActionScript have any way of handling a strongly typed list with multiple base types?

I am actually looking for something such as a Vector<T,T> ?

Is it possible?

Or is the only way of doing it is creating my own class which accepts lets say a String and Number in the constructor and create a Vector<T> out of that class?

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

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

发布评论

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

评论(2

北座城市 2024-11-02 02:43:33

不,不符合标准。如果项目不是基本类型之一,您可以构建接口向量或超类。例如,包含 MovieClip 和 Sprites(两者都继承自 DisplayObject)混合的 DisplayObjects 向量。

例如:

var v:Vector.<DisplayObject> = new <DisplayObject>[
  new MovieClip(), 
  new Sprite(), 
  new MovieClip()
];

trace(v[0].alpha); // outputs 1
trace(v[0].currentFrame); // error - not a DisplayObject property

在这种情况下,向量项将仅公开源自向量类型的自身属性和方法。但这正是您应该使用向量的原因,它确保您正在处理的项目类型。

我不知道你的具体情况或目标,但我会考虑为什么你需要向量中的混合类型。正如您所说,您的替代选择是创建一个包装类。下面的示例远未完成,但却是一个起点。

class Wrapper {
    public var _value:*; // should be private with get/set's

    public function Wrapper(value:*) {
        if(value is String || value is Number) {
            _value = value;
        }
    }
}

No, not by standard. If the items are not one of the primative types you can build a Vector of interfaces, or super classes. For example, a vector of DisplayObjects that contain a mixture of MovieClips and Sprites (which both inherit from the DisplayObject).

For example:

var v:Vector.<DisplayObject> = new <DisplayObject>[
  new MovieClip(), 
  new Sprite(), 
  new MovieClip()
];

trace(v[0].alpha); // outputs 1
trace(v[0].currentFrame); // error - not a DisplayObject property

In this case the vectors item will only expose the properties and methods of itself that stem from the Vectors type. But this is exactly the reason you should use vectors, it ensures the items type you are handling.

I don't know your specific case or goal, but I would consider why you need a mixed type within a vector. Your alternative option, as you stated, would be to create a wrapper class. The example below is far from complete but a starting point.

class Wrapper {
    public var _value:*; // should be private with get/set's

    public function Wrapper(value:*) {
        if(value is String || value is Number) {
            _value = value;
        }
    }
}
〆凄凉。 2024-11-02 02:43:33

你不能这样做,所以我会采纳你的建议,即创建一个包含两个属性(比如 Number、String)的特殊类,并创建一个它的 Vector。

You can't do that, so I would go with your suggestion, which is to create a special class containing two properties (say Number, String) and create a Vector of that.

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