如何创建特定类型的数组
我有一个类如下:
class com.flightstatus.SpecificationFlight
{
public var Airline:Airline;
public var FlightNumber:String;
public var SearchCodeshares:Boolean;
public var SearchCodesharesSpecified:Boolean;
public var TailNumber:String;
}
现在我想创建一个上述类型的数组,如下所示: var myArr:SpecificationFlight = new Array();
这种类型非常重要,因为它将通过网络服务发送。
这在 AS3 中可能吗?如果可以,我可以仅将此功能导入 AS2 并使用它吗?
I have a Class as follow:
class com.flightstatus.SpecificationFlight
{
public var Airline:Airline;
public var FlightNumber:String;
public var SearchCodeshares:Boolean;
public var SearchCodesharesSpecified:Boolean;
public var TailNumber:String;
}
Now I want to create a array of above type like below:
var myArr:SpecificationFlight = new Array();
This type is very crucial as it will be sent over webservice.
Is this possible in AS3 ?If so , can I import only this feature to AS2 and use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码是 AS2,而不是 AS3,在 AS3 中,您不能使用名称中的句点声明类,而是使用包。 (从技术上讲,这并不精确,如果您在编译字节码后更改了字节码,则名称中可能会包含句点,但我怀疑您是这个意思)。
AS2 没有类型数组,即它只有一个 Array 类,并且可以包含任何类型的对象。但是,您可以为自定义类定义
__resolve(x)
方法,并让yourClassInstance[x]
代码仅返回特定类型的对象。不幸的是,您无法控制分配给yourClassInstance[x]
的对象类型。从 Flash Player 10 开始,AS3 中有一个
Vector.
类。显然,该类用于创建某种类型的向量。在可以编译为 AS2 和 AS3 的 Haxe 语言中,存在类型化数组,但它们是在语言(编译器)级别强制执行的,如果您随后通过反射或通过其他未使用 Haxe 编译的代码访问编译后的代码,则数组仍然是无类型的(非常类似于 Java 中的泛型)。
PS。在 AS 的任一变体中使用 Pascal 大小写作为变量名称是非常违反直觉的,类字段通常是驼峰大小写的(即使代码荧光笔也假设这些是类名称,而不是变量名称)。使用公共变量通常也是一个坏主意,但这不一定是坏主意,取决于上下文,但我肯定会感到震惊。
Your code is AS2, not AS3, in AS3 you cannot declare a class using periods in the name, instead packages are used. (Technically, this is not precise, you could've have periods in the name if you altered the bytecode after it was compiled, but I doubt you meant that).
AS2 has no typed arrays, i.e. it has only one Array class and it can contain objects of any type. However, you can define a
__resolve(x)
method for your custom class and let only objects of certain type be returned fromyourClassInstance[x]
code. Unfortunately, you cannot control the type of an object being assigned toyourClassInstance[x]
.In AS3 there is a
Vector.<T>
class since Flash player 10. This class is used to create vectors of certain type, obviously.In the Haxe language, which can compile to AS2 and AS3 there are typed arrays, but they are enforced on the language (compiler) level, if you then access the compiled code through reflection or through some other code not compiled with Haxe, then arrays are still untyped (pretty much like generics work in Java).
PS. It is extremely counterintuitive to use Pascal case for variables' names in either variant of AS, class fields are usually camel-cased (even the code highlighter assumed those are class names, not variable names). Using public variable us usually a bad idea too, but it is not necessarily bad, depends on the context, yet I'd certainly be alarmed.