开发者

Strongly typed collection with multiple base types in ActionScript (Vector <T,T>)?

Does ActionScript have any way of handling a strongly typed list with multiple base ty开发者_开发问答pes?

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜