views:

80

answers:

3

In AS3, I'm trying 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?

A: 

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 */ }
PatrickS
+3  A: 
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{}
OXMO456
Thanks, I might write a utility function based on this. It seems this is as close as I'm going to get!
Rowan
A: 
package
{   
import flash.display.Sprite;
import flash.utils.getQualifiedSuperclassName;

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