您可以使用该constructor
属性获取对实例自己的类的引用,但要访问祖先类,您必须使用describeType
and 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;
}