假设我有以下课程:
class Example
{
function set something(value:String):void
{
trace("set something");
}
function doSomething():void
{
trace("something");
}
}
我可以像下面那样访问函数:
var example:Example = new Example();
var asdf:Function = example.doSomething;
// this also works - example["doSomething"];
asdf(); // this trace: "something"
例如,您可以随时使用事件执行此操作。所以,我最大的问题是:有没有办法解决这个问题?在Object或某个我不知道的地方有一些疯狂的功能(请说是的:)
我想要类似下面的内容
var example:Example = new Example();
// the following won't work, because example.something is a string
var asdf:Function = example.something;
asdf("a value"); // this trace: "something"
答案 0 :(得分:1)
语句var asdf:Function = example.something;
将不起作用,因为编译器将example.something
视为getter并返回字符串(如果未实现getter,则抛出只写错误)。
但由于something
是一个属性,你可以这样做:
example["something"] = "a value"; //will trace 'set something'
//or
var property:String = "something";
example[property] = "some value"; //will trace 'set something'
答案 1 :(得分:0)
你可以试试这个:
class Example{
function set something(value:String):void{
trace("set something");
}
function doSomething():void{
trace("something");
}
}
class AnotherClass{
function callOtherClassFunction(funcObj:Obj):void{
if (funcObj.type == "method") {
funcObj.func.apply();
}
else if (funcObj.type == "setter") {
funcObj.obj[funcObj.func] = "something";
}
else if (funcObj.type == "getter") {
trace(funcObj.obj[funcObj.func]);
}
}
}
class Test{
function Test():void{
var e:Example = new Example();
var a:AnotherClass = new AnotherClass();
a.callOtherClassFunction({obj:e, type:"setter", func:"something"});
a.callOtherClassFunction({obj:e, type:"method", func:e.doSomething});
}
}