类型/类实例上的开关/大小写?

发布于 2024-11-05 06:20:38 字数 364 浏览 2 评论 0原文

我有一组不同的 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 技术交流群。

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

发布评论

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

评论(4

醉酒的小男人 2024-11-12 06:20:38

您可以获取字符串形式的类名,然后像通常使用此方法一样对其进行切换...

switch (getQualifiedClassName(item)) {
 case "Pink":
 // do something
 break;

 case "Red":
 // do something
 break;
}

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...

switch (getQualifiedClassName(item)) {
 case "Pink":
 // do something
 break;

 case "Red":
 // do something
 break;
}

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getQualifiedClassName()

唠甜嗑 2024-11-12 06:20:38

简单的回答:不。

PinkRed都是Color,所以让Color有一个功能:

interface IColor
{
  public function doSomething():void;
}

并且有Pink code> 和 Red 扩展了函数:

class Pink extends MovieClip implements IColor
{
  ...
  public override function doSomething():void
  {
    //different code
  }
}

class Red extends MovieClip implements IColor
{
  ...
  public override function doSomething():void
  {
    //more different code
  }
}

然后在你的代码中你可以调用:

item.doSomething();

它会为任何一种情况做正确的事情。

Simple answer: don't.

Pink and Red are both Colors so make Color have a function:

interface IColor
{
  public function doSomething():void;
}

and have Pink and Red extend the function:

class Pink extends MovieClip implements IColor
{
  ...
  public override function doSomething():void
  {
    //different code
  }
}

class Red extends MovieClip implements IColor
{
  ...
  public override function doSomething():void
  {
    //more different code
  }
}

then in your code you can just call:

item.doSomething();

and it will do the right thing for either case.

你丑哭了我 2024-11-12 06:20:38

这个问题已经有了答案,但对于任何有兴趣的人,您也可以这样做:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var cat:Cat = new Cat();

            switch(Class(Object(cat).constructor))
            {
                case Cat : trace("instance of " + Cat); break;
                case Dog : trace("instance of " + Dog); break;

            }// end switch

            // output: 

        }// end function

    }// end class

}// end package

internal class Cat
{ 
    public function Cat() { }

}// end class

internal class Dog
{ 
    public function Dog() { }

}// end class

There's already an answer for this question but for anyone who is interested you can also do this:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var cat:Cat = new Cat();

            switch(Class(Object(cat).constructor))
            {
                case Cat : trace("instance of " + Cat); break;
                case Dog : trace("instance of " + Dog); break;

            }// end switch

            // output: 

        }// end function

    }// end class

}// end package

internal class Cat
{ 
    public function Cat() { }

}// end class

internal class Dog
{ 
    public function Dog() { }

}// end class
不…忘初心 2024-11-12 06:20:38

使用“is”关键字:

if (item is Pink) // do something

在 case 语句中使用“is”看起来像这样:

switch(true)
{
    case item is Pink :
        // do something
        break;

    case item is Red :
        // do something
        break;
}

“is”语句的强大之处在于它适用于继承类型。因此,举例来说,如果我想检查 MovieClip、Sprite 或 SimpleButton,我可以这样写:

if (item is DisplayObject) // do something

因为所有这些类型都继承自 DisplayObject。

另一个好处是没有不必要的内省(例如使用 getQualifiedClassName)。因此,“is”关键字具有更好的性能,并且不需要额外的代码。

Use the “is” keyword:

if (item is Pink) // do something

Using “is” in a case statement would look like this:

switch(true)
{
    case item is Pink :
        // do something
        break;

    case item is Red :
        // do something
        break;
}

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:

if (item is DisplayObject) // do something

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.

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