我不确定这是否可能,所以我有这样的方法
callMeMethod(String param1)我想把这个方法叫做
param1.callMeMethod()但是当我尝试这样做的时候我会犯一个错误
Unhandled Error: No signature of method: java.lang.String.callMeMethod() is applicable for argument types: () values: []发布于 2016-06-24 19:02:39
这是可能的,但您必须将其添加到param的metaClass (在您提供的示例中的字符串)。你能做到这一点的方法如下所示。
String.metaClass.callMeMethod = {-> println "function code can go here"}
String param1 = "meParam"
param1.callMeMethod()
output-> function code can go here我也会看看类别
或特征作为可能更合适的替代品
https://stackoverflow.com/questions/37913772
复制相似问题