开发者

AS3 - Find the base class of a sub class

开发者 https://www.devze.com 2023-02-25 20:07 出处:网络
I have a program where I have multiple classes derived from a base class. I want to take all instances of the different subclasses and put them into an array. Is there a way I can search for anything

I have a program where I have multiple classes derived from a base class. I want to take all instances of the different subclasses and put them into an array. Is there a way I can search for anything derived from the base class?

For example, right now a get the number of children, then see what they are, so:

for (var i = 0; i < this.getNumChildren(); i++)
{
    if (i is "type")
        objectArray.push(i);
}

Where type is, I need it to refer to the base c开发者_如何学运维lass of the instance not the subclass that created the instance.


"Object is Type" will return true for both the SubClass and the BaseClass:

Example:

for (var i = 0; i < this.getNumChildren(); i++) {
    var c:* = this.getChildAt(i);
    if ( c is BaseClass ) {
        //true for both SubClass1 and SubClass2 instances
        objectArray.push(c);
        if ( c is SubClass1 ) {

        }
        else if ( c is SubClass2 ) {

        }
    }
}


I'm guessing at what you're asking: Let's assume that you've got this code in class B which extends class A, and what you want is for the instance of type B to find out what its own superclass is (class A) at runtime.

Here is how you can do that:

// Get the name of the super class for this object
var superClassName:String = flash.utils.getQualifiedSuperclassName(this);

// Get a Class reference to that superclass
var superClass:Class = flash.utils.getDefinitionByName(superClassName) as Class;

use superClass in place of "type" in your expression. This will allow the instance of type B to find all objects that share ancestry of type A.

Is this what you were asking? If not, please clarify. It's not clear to me if you are asking how to get the base class, or if you just misunderstand how the is operator works. If you know the superclass name at authoring time, why wouldn't you just put that in place of "type"?

0

精彩评论

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