How to detect if a class is a subclass?


#1

Hello,

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

Thanks


#2

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


#3

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

#4

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


#5

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


#6

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