在ActionScript上有可能有可调用的对象吗?例如:
class Foo extends EventDispatcher
{
Foo() { super(); }
call(world:String):String
{
return "Hello, " + world;
}
}后来..。
var foo:Foo = new Foo();
trace( foo("World!") ); // Will NOT work发布于 2010-05-01 08:21:07
正如其他人所说,你不能有可调用的对象。但是,如果出于某种原因您想要有状态函数,您可以通过静态类变量和包级函数来实现。例如:
// com/example/foo/Helper.as
package com.example.foo {
public class Helper {
private static var _instance:Foo;
public static var data:String;
public static function get instance():Helper
{
if(!_instance) { _instance = new Helper(); }
return _instance;
}
}
}
// com/example/foo/hello.as
package com.example.foo {
public function hello(world:String):void
{
if(Helper.instance.data)
{
trace("Bye, " + Helper.instance.data);
}
trace("Hello, " + world);
Helper.instance.data = world;
}
}使用时,它将打印不同的内容。
hello("World!"); // traces "Hello, World!"
hello("People"); // traces "Bye, World!" and "Hello, People"https://stackoverflow.com/questions/2731709
复制相似问题