지금 Groovy 클로저와 델리게이트를 실험하고 있습니다. 다음 코드는 클로저의 대리자를 다른 클래스에 설정하여 완벽하게 작동합니다.Groovy에서 MethodClosure의 델리게이트 설정
def closure = {
newMethod()
};
closure.setDelegate(new MyDelegate());
closure.setResolveStrategy(Closure.DELEGATE_ONLY);
closure();
class MyDelegate {
public void newMethod() {
println "new Method";
}
}
이것은 MyDelegate에서는 newMethod()가 사실이라는 것을 보여주는, "새로운 방법"을 출력합니다. 이제 MethodClosure를 사용하여 동일한 작업을 수행하려고합니다. 스레드 "주요"groovy.lang.MissingMethodException에서 예외 : 방법 없음 서명 : TestClass.newMethod()는 인수 유형에 대한 적용 : 그러나
public class TestClass {
public void method() {
newMethod();
}
}
TestClass a = new TestClass();
def methodClosure = a.&method;
methodClosure.setDelegate(new MyDelegate());
methodClosure.setResolveStrategy(Closure.DELEGATE_ONLY);
methodClosure();
class MyDelegate {
public void newMethod() {
println "new Method";
}
}
,이 시간, 나는 다음과 같은 예외가() 값 : [].
그래서이 methodClosure의 경우 메서드 조회를 위해 위임자로가는 것처럼 보이지 않습니다. 아마 이것이 의도 된 행동이라고 생각하지만 실제로 MethodClosures에 대리자를 사용하는 방법이 있습니까?
고맙습니다.
내가 원하는 정확히 무엇을합니까. 완전한! – user872831