Actionscript 3 如何在ActionScript对象上调用静态函数';谁的祖先阶级?

Actionscript 3 如何在ActionScript对象上调用静态函数';谁的祖先阶级?,actionscript-3,reflection,actionscript,static,Actionscript 3,Reflection,Actionscript,Static,祖先类确实有一个名为“foo”的函数(为了示例) 公共静态函数callAncestorStaticMethod():void { var ancestorClassName:String=getQualifiedSuperclassName(后代); var ancestorClass:Class=Class(getDefinitionByName(ancestorClassName)); ancestorClass.foo();//您可以使用构造函数属性获取对实例自身类的引用,但要访问祖先类,

祖先类确实有一个名为“foo”的函数(为了示例)

公共静态函数callAncestorStaticMethod():void
{
var ancestorClassName:String=getQualifiedSuperclassName(后代);
var ancestorClass:Class=Class(getDefinitionByName(ancestorClassName));

ancestorClass.foo();//您可以使用
构造函数
属性获取对实例自身类的引用,但要访问祖先类,您必须使用
describeType
getDefinitionByName
。这些功能强大,但成本高昂-因此请确保不要过度使用:

function callStaticAncestorProperty( instance:Object, staticProperty:String ):* {
    var type:XML = describeType( instance );
    var ret:* = instance.constructor[staticProperty];
    for each(var extend:XML in type.extendsClass) 
        ret = ret ? ret : getStaticPropertyOrUndefined( extend, staticProperty );  
    return ret;
}

function getStaticPropertyOrUndefined( extend:XML, staticProperty:String ):* {
    var clazz:Class = getDefinitionByName( extend.@type.toString().replace( "::", "." ) ) as Class;
    return clazz[staticProperty] ? clazz[staticProperty] : undefined;
}
这将检查类本身是否具有该属性,然后对每个超类型进行迭代。请注意,将返回要找到的第一个值,即,如果子类和超类都具有该属性,则将返回子类的属性

编辑

我才意识到你问的是方法调用,而不是属性。这和你问的差不多:

function callStaticAncestorMethod( instance:Object, staticMethod:String ):void {
    var type:XML = describeType( instance );
    var method:Function = instance.constructor[staticMethod];
    for each(var extend:XML in type.extendsClass) 
        method = method ? method : getStaticMethodOrUndefined( extend, staticMethod );  
    if (method) method();
}

function getStaticMethodOrUndefined( extend:XML, staticMethod:String ):Function {
    var clazz:Class = getDefinitionByName( extend.@type.toString().replace( "::", "." ) ) as Class;
    return clazz[staticMethod] ? clazz[staticMethod] : undefined;
}

我能够使用以下代码调用超类中的静态函数:

var c:ChildClass = new ChildClass();
var s:String = getQualifiedSuperclassName(c);
var cl:Class = getDefinitionByName(s) as Class;

cl.foo.call();  
//cl["foo"].call();
类对象具有类的所有静态属性和方法,因此这应该是可靠的。
cl.foo
返回一个函数对象,然后您可以
.call()
或(基于Sam DeHaan的答案):

如果超类和子类都有字符串id属性

(getDefinitionByName(getQualifiedSuperclassName(Descendant))as Class).foo();
trace((getDefinitionByName(getQualifiedSuperclassName(Descendant))as Class).id);
其中:

// trace (Descendant.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var d:Descendant;
trace((getDefinitionByName("Descendant") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Descendant") as Class).foo();
// Call static foo() from Descendant. // Throw a compile time Error if method is private

// trace (Superclass.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var s:Superclass;
trace((getDefinitionByName("Superclass") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Superclass") as Class).foo();
// Call static foo() from Superclass. // Throw a compile time Error if method is private

秘密握手是。call()
// trace (Descendant.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var d:Descendant;
trace((getDefinitionByName("Descendant") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Descendant") as Class).foo();
// Call static foo() from Descendant. // Throw a compile time Error if method is private

// trace (Superclass.id);
// if private : compile time Error.
// 1178: Attempted access of inaccessible property id through a reference with static type Class.
var s:Superclass;
trace((getDefinitionByName("Superclass") as Class).id);
// output undefined if private : the value if public. But don't throw compile time Error.
(getDefinitionByName("Superclass") as Class).foo();
// Call static foo() from Superclass. // Throw a compile time Error if method is private