Actionscript 3 确定是否使用super调用ActionScript方法
我有一个方法,每次调用它时都会记录一条消息。我希望此日志消息指示是直接调用该方法还是在子类中使用Actionscript 3 确定是否使用super调用ActionScript方法,actionscript-3,super,Actionscript 3,Super,我有一个方法,每次调用它时都会记录一条消息。我希望此日志消息指示是直接调用该方法还是在子类中使用super调用该方法 class-DoerOfWork{ 公共函数doWork():void{ var calledWithSuper:布尔值; 调用WithSuper=???; 跟踪(“称为“+(称为WithSuper?”(带super)。”:“); } } 类SlowerDoerOfWork扩展了DoerOfWork{ 公共重写函数doWork():void{ 对于(变量i:Number=0;i
super
调用该方法
class-DoerOfWork{
公共函数doWork():void{
var calledWithSuper:布尔值;
调用WithSuper=???;
跟踪(“称为“+(称为WithSuper?”(带super)。”:“);
}
}
类SlowerDoerOfWork扩展了DoerOfWork{
公共重写函数doWork():void{
对于(变量i:Number=0;i<321684;i++){
//等一下
}
super.doWork();
}
}
我希望通过将this.doWork
与DoerOfWork.prototype.doWork
进行比较,可以确定this
类是否覆盖了doork
的实现
class DoerOfWork {
public function doWork():void {
var calledWithSuper:Boolean;
calledWithSuper = this.doWork == arguments.callee;
trace("doWork called" + (calledWithSuper ? " (with super)." : "."));
}
}
不幸的是,这是不可能的。在ActionScript中的任何地方都无法访问未绑定方法(规范列出了两种类型的函数:函数闭包和绑定方法)。MethodClosure
上的实例上甚至没有任何属性可以识别两个是否是同一方法的绑定副本
如何检查某个方法是否已被重写或使用任何其他方法来确定当前正在执行的ActionScript方法是使用
super
调用的还是直接调用的?您可以将当前正在执行的函数的引用作为参数。被调用方
。从方法中,这将是DoerOfWork().doWork()
与this
的MethodClosure
。如果未覆盖doWork()
,则此值将等于this.doWork
class DoerOfWork {
public function doWork():void {
var calledWithSuper:Boolean;
calledWithSuper = this.doWork == arguments.callee;
trace("doWork called" + (calledWithSuper ? " (with super)." : "."));
}
}
如果在非严格模式下运行,显然这将禁用当前函数的参数计数检查。(我自己还没有检查过,我甚至不知道如何在IntelliJ中禁用严格模式。)如果您愿意使用
flash.utils.describeType()
,它包含我们需要的信息:
XML中每个
的declaredBy
属性使用与flash.utils.getQualifiedClassName
相同的格式,我们可以比较这些属性以确定是否覆盖了实现
class DoerOfWork {
public function doWork():void {
var calledWithSuper:Boolean;
var currentImplementationFrom:String
= flash.utils.describeType(this).method.(@name=="doWork").@declaredBy;
var thisImplementationFrom:String
= flash.utils.getQualifiedClassName(DoerOfWork);
calledWithSuper = currentImplementationFrom != thisImplementationFrom;
trace("doWork called" + (calledWithSuper ? " (with super)." : "."));
}
}