groovy的错误

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

class MethodClosureSample {
	int limit
	
	
	public 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.&validate
		
		def words = ['long string','medium','short','tiny']
		
		assert 'medium' == words.find(firstClosure)
		assert 'short' == words.find(second.&validate)
		
	}

}

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

class MethodClosureSample {
	int limit
	
	
	public 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.&validate
		
		def words = ['long string','medium','short','tiny']
		
		assert 'medium' == words.find(firstClosure)
		assert 'short' == words.find(second.&validate)
		
	}

}





你可能感兴趣的:(groovy)