How to detect if a class is a subclass?

Hello,

how to detect if a class is a subclass of another class ?

Thanks

You can use Lib.getqualifiedSuperClassName()
For example:

package;

import openfl.display.Sprite;
import openfl.Lib;

class Main extends Sprite
{

	public function new()
	{
		super();
		var mc:CustomSprite = new CustomSprite();
		trace(Lib.getQualifiedSuperclassName(mc));
	}

}

class CustomSprite extends Sprite
{
	public function new()
	{
		super();
	}

}

will output Main.hx:17: openfl.display.Sprite

you can also use Std.is()

class Test {
    static function main() {
        var bar = new Bar();
        trace(Std.is(bar, Foo));
    }
}
class  Foo { }
class Bar extends Foo {
    public function new() { }
}

Std.is() won’t tell you which base class the object extends.

correct, but he asked “how to detect if a class is a subclass of another class”
not “how to find the subclass of another class”

I read that as how to find out if instance A is subclass B

Aah - I see what you mean. Well, the question can be interpreted in multiple ways. :wink:

1 Like