开发者

Switch/Case on a Type/Class instance?

开发者 https://www.devze.com 2023-03-03 04:54 出处:网络
I have a set of different MovieClips: Pink Yell开发者_Go百科ow Red and I create an item item = new Pink();

I have a set of different MovieClips:

Pink
Yell开发者_Go百科ow
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...


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()


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.


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


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.

0

精彩评论

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

关注公众号