Groovy内部类不适用于Apache Wicket

时间:2014-05-20 09:43:38

标签: groovy wicket wicket-6

我试图用Apache Wicket(6.15.0)和Groovy(2.2.2或2.3.1)编写简单的东西。而我在内部课程方面遇到了麻烦。

class CreatePaymentPanel extends Panel { 
  public CreatePaymentPanel(String id) {
    super(id)
    add(new PaymentSelectFragment('currentPanel').setOutputMarkupId(true))
}

public class PaymentSelectFragment extends Fragment {
        public PaymentSelectFragment(String id) {
            super(id, 'selectFragment', CreatePaymentPanel.this) // problem here
            add(new AjaxLink('cardButton') {
                @Override
                void onClick(AjaxRequestTarget target) {
                    ... CreatePaymentPanel.this // not accessible here 
                }
            })
            add(new AjaxLink('terminalButton') {
                @Override
                void onClick(AjaxRequestTarget target) {
                    ... CreatePaymentPanel.this // not accessible here 
                }
            });
        }
        } // end of PaymentSelectFragment class
} // end of CreatePaymentPanel class

Groovy试图在CreatePaymentPanel类中找到属性“this”..如何解决这个问题?它是一个有效的java代码,但不是groovy。

然而, Test.groovy:

class Test {

    static void main(String[] args) {
        def a = new A()
    }

    static class A {
        A() {
            def c = new C()
        }

        public void sayA() { println 'saying A' }

        class B {
            public B(A instance) {
                A.this.sayA()
                instance.sayA()
            }
        }
        /**
         * The problem occurs here
         */
        class C extends B {
            public C() {
                super(A.this) // groovy tries to find property "this" in A class
                sayA()
            }
        }
    }
}

上面的代码不起作用,就会出现同样的错误,比如Wicket的情况。

和TestJava.java一样,工作:

public class TestJava {

    public static void main(String[] args) {
        A a = new A();
    }

    static class A {
        A() {
            C c = new C();
        }

        public void sayA() {
            System.out.println("saying A");
        }

        class B {
            public B(A instance) {
                instance.sayA();
            }
        }

        /**
         * This works fine
         */
        class C extends B {
            public C() {
                super(A.this);
                sayA();
            }
        }
    }
}

我缺少什么?

1 个答案:

答案 0 :(得分:0)

您无法在CreatePaymentPanel.this内引用PaymentSelectFragment,因为此处无法访问CreatePamentPanel的{​​{1}}个实例。如果允许的话,您期望评估什么?