AS3反映。如何找出一个方法被重写? [英] AS3 reflection. How to find out if a method is overridden?

查看:304
本文介绍了AS3反映。如何找出一个方法被重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用AS3反思,找出一个方法被重写?

Is it possible to use AS3 reflection to find out if a method was overridden?

我需要像一个方法:

protected function isOverriden(methodName:string) : bool
{
    //magic here!
    //...

    return awesomeLocalVariable;
}

所以,我通过在方法名称作为字符串和isOverridden方法产生真实的,只有当且仅当对象具有该名称的方法,它是从原来的实现覆盖。

So, I pass in the method name as a string and the isOverridden method yields true only if and only if the object has a method of that name and it is overridden from its original implementation.

这是不知道如何code的神奇呢?

Any idea on how to code the magic there?

感谢。

编辑:作为请求,问题的背景下:

As requested, the context of the problem:

我建立一个框架,用于创建AS3游戏。我要为我的游戏对象提供部件,每个组件提供的功能,它被应用在游戏对象。组件都基于事件(的onClick,OnUpdate中,onShapeCollision等),我需要这个code在组件类,这样我就可以只注册了事件的实际组件派生类实现(覆盖)。

I'm building a framework for creating AS3 games. I want to provide "components" for my game objects, each component provides functionality to the game object it is applied. Components are based on events (onClick, onUpdate, onShapeCollision, etc) I need this code in the Component class, so I can register only the events that the actual Component-derived class implements (overrides).

例组件:

    public class CTrace extends ScriptComponent
    {
            public override function onClick(event:MouseEvent = null):void
            {
                    trace(Owner.Id);
            }
    }

框架应当登记的onClick方法作为事件处理MouseEvent.CLICK事件,因为它覆盖了默认的实现。

The framework should register the onClick method as the event handler for the MouseEvent.CLICK event because it overrides the default implementation.

为什么我需要的默认实现?因为我想的类重写支持的方法,因此,如果用户试图使用不支持的情况下会有一个编译时错误。

Why do I need the default implementation? Because I want the classes to override the supported methods so there will be a compile time error if the user tries to use an unsupported event.

这是否有道理?

推荐答案

下面是一个尝试。该功能是静态的,它可用于检查任何类或对象而不管其所实施的类的。如果你给它的类型,它会使用它,如果你给它的一个实例,它会得到的类型本身。的内在逻辑就是检查给定的类型说明,我们正在寻找的功能,如果这样的存在,是由类中声明,它会检查方法也存在于父母。如果双方存在,享受,这意味着它将被重写。

Here is a try. The function is static and it may be used to check any class or object regardless of the class in which it is implemented. If you give it the type, it will use it, if you give it an instance, it will get the type by itself. The inner logic is just to check the given type description for the function we are looking for, if such exists and is declared by the class, it will check if the method also exists in the parent. And if both exists, enjoy, it means it is overridden.

/**
 * Returns true only if the method name given is declared by
 * the source class, and any parent class.
 */
static public function isOverridden(source:*, methodName:String):Boolean {
	var parentTypeName:String = getQualifiedSuperclassName(source);
	if (parentTypeName == null) {
		return false;
	}//if

	var typeName:String = getQualifiedClassName(source);
	var typeDesc:XML = describeType(getDefinitionByName(typeName));
	var methodList:XMLList = typeDesc.factory.method.(@name == methodName);

	if (methodList.length() > 0) {
		//Method exists
		var methodData:XML = methodList[0];
		if (methodData.@declaredBy == typeName) {
			//Method is declared in self
			var parentTypeDesc:XML = describeType(getDefinitionByName(parentTypeName));
			var parentMethodList:XMLList = parentTypeDesc.factory.method.(@name == methodName);
			return parentMethodList.length() > 0;
		}//if
	}//if

	return false;
}//isOverridden

和公正的情况下,它是必要的,需要进口,它的工作:

And just in case it is needed, the imports required for it to work:

import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;

和使用它:

trace(isOverridden(ChildrenClass, "overriddenMethod")); //true
trace(isOverridden(ChildrenClass, "onlyChildMethod")); //false
trace(isOverridden(ChildrenClass, "onlyParentMethod")); //false

这篇关于AS3反映。如何找出一个方法被重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆