类型/类实例上的开关/大小写?
我有一组不同的 MovieClip:
Pink
Yellow
Red
并且我创建了一个项目
item = new Pink();
item = new Red();
等...
我如何编写一个 switch case 来查看我拥有哪个 MovieClip?
switch (item) {
case Pink:
// do something
break;
case Red:
// do something
break;
}
我只知道如何为字符串编写 switch case...
I have a set of different MovieClips:
Pink
Yellow
Red
and I create an item
item = new Pink();
item = new Red();
etc...
How do I write a switch case to see which MovieClip I have?
switch (item) {
case Pink:
// do something
break;
case Red:
// do something
break;
}
i only know how to write switch cases for Strings...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以获取字符串形式的类名,然后像通常使用此方法一样对其进行切换...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getQualifiedClassName()< /a>
You can get the class name as a string and do a switch on that as you normally would using this method...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getQualifiedClassName()
简单的回答:不。
Pink
和Red
都是Color
,所以让Color
有一个功能:并且有
Pink
code> 和Red
扩展了函数:然后在你的代码中你可以调用:
它会为任何一种情况做正确的事情。
Simple answer: don't.
Pink
andRed
are bothColor
s so makeColor
have a function:and have
Pink
andRed
extend the function:then in your code you can just call:
and it will do the right thing for either case.
这个问题已经有了答案,但对于任何有兴趣的人,您也可以这样做:
There's already an answer for this question but for anyone who is interested you can also do this:
使用“is”关键字:
在 case 语句中使用“is”看起来像这样:
“is”语句的强大之处在于它适用于继承类型。因此,举例来说,如果我想检查 MovieClip、Sprite 或 SimpleButton,我可以这样写:
因为所有这些类型都继承自 DisplayObject。
另一个好处是没有不必要的内省(例如使用 getQualifiedClassName)。因此,“is”关键字具有更好的性能,并且不需要额外的代码。
Use the “is” keyword:
Using “is” in a case statement would look like this:
The powerful thing about the “is” statement is that works for inherited types. So, for instance, if I wanted to check for a MovieClip, a Sprite or a SimpleButton, I could just write:
Since all these types inherit from DisplayObject.
Another benefit is that there is no unnecessary introspection (such as with getQualifiedClassName). Thus, the “is” keyword has far better performance and requires no additional code.