Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何从字符串创建闭包?
就像我有一个字符串def formula = "{it * it}"
def formula = "{it * it}"
现在公式是一个字符串,我不能做公式()
如何将字符串变量公式中的代码块定义为闭包?
你可以做:
def formula = Eval.me( "{ x -> x * x}" ) formula( 3 )
显然,关于评估用户输入的字符串的安全性的常见警告适用
您可能还对昨天提出的几乎相同的问题感兴趣