开发者

AS3 check if class extends another class

In AS3, I'm t开发者_如何学JAVArying to check whether an object is an instance of, or extends a particular class. Using something like if (object is ClassName) works fine if the object is an instance of ClassName but not if it's an instance of a class that extends ClassName.

Pseudo-code example:

class Foo {}
class Bar extends Foo {}

var object = new Bar();

if (object is Foo){ /* not executed */ }
if (object is Foo){ /* is executed */ }

I want something like:

class Foo {}
class Bar extends Foo {}

var object = new Bar();

if (object is Foo){ /* is executed */ }

Any ideas anyone?


package {
 import flash.display.Sprite;



public class Main extends Sprite {
  public function Main() {
   var bar:Bar=new Bar();
   trace("bar is Bar",bar is Bar);//true
   trace("bar is Foo:",bar is Foo);//true
   trace("bar is IKingKong:",bar is IKingKong);//true
   trace(describeType(bar));
   //<type name="Main.as$0::Bar" base="Main.as$0::Foo" isDynamic="false" isFinal="false" isStatic="false">
   //<extendsClass type="Main.as$0::Foo"/>
   //<extendsClass type="Object"/>
   //<implementsInterface type="Main.as$0::IKingKong"/>
   //</type>
  }
 }
}
interface IKingKong{}
class Foo implements IKingKong{}
class Bar extends Foo{}


You can do this:

class Foo {}
class Bar extends Foo {}

var object = new Bar();

if (object as Foo != null) { /* is executed */ }


Using an interface or an abtract class , you should be able to do this

 var object:Foo = new Bar();
 if (object is Foo){ /* is executed */ }

 //or
 var object:IFoo = new Bar();
 if (object is IFoo){ /* is executed */ }


package
{   
import flash.display.Sprite;
import flash.utils.getQualifiedSuperclassName;

public class Test extends Sprite
    {
    public function Test()
        {
        trace(getQualifiedSuperclassName(this)); //returns "flash.display::Sprite"
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜