Ancestor类确实有一个函数叫(为了举例)“foo”。
public static function callAncestorStaticMethod() : void
{
var ancestorClassName : String = getQualifiedSuperclassName(Descendant);
var ancestorClass : Class = Class(getDefinitionByName(ancestorClassName));
ancestorClass.foo(); // <---- runtime error here: foo is not a function
}
检查ancestorClass会发现它是一个没有可见属性的对象(ancestorClass.prototype也没有)。
那么,当我在运行时只将其名称作为字符串时,如何在类上调用静态函数?
答案 0 :(得分:4)
我能够使用以下代码在超类中调用静态函数:
var c:ChildClass = new ChildClass();
var s:String = getQualifiedSuperclassName(c);
var cl:Class = getDefinitionByName(s) as Class;
cl.foo.call();
//cl["foo"].call();
Class对象具有Class的所有静态属性和方法,因此这应该是可靠的。 cl.foo
返回一个Function对象,然后您可以.call()
。
答案 1 :(得分:1)
您可以使用constructor
属性获取对实例自己的类的引用,但要访问祖先类,您必须使用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;
}
答案 2 :(得分:0)
或(基于Sam DeHaan回答):
如果Superclass和Descendant都有String 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