[英]Can I change a function reference of a class in ActionScript-3?
Ok, I have a class like this: 好的,我有一个像这样的课程:
public class Foo extends Sprite {
public function Foo(x:Number, y:Number):void {
this.x = x;
this.y = y;
}
public function bar():void {
trace("I'm a happy class.");
}
}
And I want to do something like this: 我想做这样的事情:
var foo:Foo = new Foo();
foo.bar = function():void {
trace("I'm a happier class.");
}
I'm getting this error from the compiler: "Error: Illegal assignment to function bar". 我从编译器中收到此错误:“错误:对功能栏的非法分配”。 How can I change the public function bar on the fly?
如何动态更改公共功能栏?
You can't do that in ActionScript. 您无法在ActionScript中做到这一点。 There is a work around though, try something like this:
但是,有一种解决方法,请尝试如下操作:
public dynamic class Foo{
public function Foo() {
this.bar = function():void { trace("bar"); }
}
}
var f:Foo = new Foo();
f.bar();
f.bar = function():void { trace("baz"); }
f.bar();
EDIT: OR THIS 编辑:或此
public class Foo{
public var bar:Function;
public function Foo() {
this.bar = function():void { trace("bar"); }
}
}
var f:Foo = new Foo();
f.bar();
f.bar = function():void { trace("baz"); }
f.bar();
Goodluck! 祝好运!
我认为该类需要声明为dynamic
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.