1. call()方法 与 Function.apply()方法
// 实际上, 去除'implement Function' 以下代码仍然有效
// 因为 call()方法不是 抽象类Function所拥有的方法
class Adder implements Function {
call(int a, int b) => a + b;
}
class Incrementer implements Function {
int _amt;
Incrementer(this._amt);
call(int a) => a + _amt;
}
Function f = (int n, int m, {operation: "add"}) {
if (operation == "add") {
return n + m;
} else {
return n - m;
}
};
main() {
Adder myAdder = new Adder();
Incrementer myIncrementer = new Incrementer(2);
// 这里就是call()方法的神奇之处了, 直接通过实例就能调用call()方法
print(myAdder(10, 3));
print(myIncrementer(40));
int a = Function.apply(f, [10,3]);
print(a);
int b = Function.apply(f, [10,3], {new Symbol("operation"): "subtract"});
print(b);
}