ActionScript 3 - 在声明时返回/检查类值
我想知道是否有任何方法可以模仿我们在 AS3 中顶级类的相同行为,例如:
var myArray:Array = [1,2,3,4];
trace(myArray) // [1,2,3,4];
如您所见,它在跟踪时返回自己的对象。
但如果我创建自己的类来扩展数组,我会发现
var queue:Queue = new Queue([1,2,3,4]);
trace(queue) // no output
这里有两个问题。
是否可以创建一个自定义类实例,就像我创建一个数组一样:
var queue:Queue = [1,2,3,4];
//instead
var queue:Queue = new Queue([1,2,3,4]);
以及如何在请求对象时返回超级对象;
trace(queue) // [1,2,3,4];
我不确定这是否可以在 AS3 中完成,
谢谢您的帮助
I'm wondering if is there any way to mimic the same behaviour we have for top level classes in AS3 for example:
var myArray:Array = [1,2,3,4];
trace(myArray) // [1,2,3,4];
As you can see it returns the own object when tracing it.
but if I create my own class that extends Array
I get
var queue:Queue = new Queue([1,2,3,4]);
trace(queue) // no output
so there are 2 questions here.
is it possible to create a custom class instance just like I create an Array like:
var queue:Queue = [1,2,3,4];
//instead
var queue:Queue = new Queue([1,2,3,4]);
and how can I return the super object when asking for the object like;
trace(queue) // [1,2,3,4];
I'm not sure if this is possible to do in AS3
thanks for you help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 livedocs.adobe.com,
“您可以扩展数组但是,您必须将子类指定为动态,否则您将失去在数组中存储数据的能力。”
也许您没有显式地将子类指定为动态(声明为公共动态类队列)?
According to livedocs.adobe.com,
"You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array."
Perhaps you're not explicitly specifying the subclass as dynamic (declared as
public dynamic class Queue
)?@jamie-wong 在 此处指出了 @quasimondo 回复的正确答案。
这不是我的答案,而是@quasimondo 的答案,但由于无法将评论分类为正确答案,所以我更愿意回答这个问题。
所有学分都归他们两人所有。
@jamie-wong pointed the right answer replied by @quasimondo right here.
This isn't an answer of mine but for @quasimondo, but since there's no way to classify a comment as the right answer I'd prefer to have this question replied instead.
All credits go for both of them.
最好的方法是重写类的 toString 方法,当您将类与字符串连接或只是跟踪它时,该方法可以输出您想要的任何内容。
这会输出:
The best way to do this is to override the toString method of the class which can be made to output whatever you want when you concatenate the class with a string or just trace it.
Which would output: