groovy的错误

来源:互联网 发布:老司机网站 知乎 编辑:程序博客网 时间:2024/06/05 12:40

1.Cannot cast object 'org.codehaus.groovy.runtime.MethodClosure@92d6d2' with class 'org.codehaus.groovy.runtime.MethodClosure' to class 'Closure'

class MethodClosureSample {int limitpublic MethodClosureSample(int limit) {this.limit = limit}boolean validate(String value){return value.length() <= limit}static main(args) {MethodClosureSample first = new MethodClosureSample(6)MethodClosureSample second = new MethodClosureSample(5)Closure firstClosure = first.&validatedef words = ['long string','medium','short','tiny']assert 'medium' == words.find(firstClosure)assert 'short' == words.find(second.&validate)}}

如上代码改成如下的形式:

class MethodClosureSample {int limitpublic MethodClosureSample(int limit) {this.limit = limit}boolean validate(String value){return value.length() <= limit}static main(args) {MethodClosureSample first = new MethodClosureSample(6)MethodClosureSample second = new MethodClosureSample(5)org.codehaus.groovy.runtime.MethodClosure firstClosure = first.&validatedef words = ['long string','medium','short','tiny']assert 'medium' == words.find(firstClosure)assert 'short' == words.find(second.&validate)}}





原创粉丝点击