编程语言
首页 > 编程语言> > Java-Groovy中的匿名内部类

Java-Groovy中的匿名内部类

作者:互联网

我正在研究groovy-wicket集成,并且在编写事件处理程序时缺少匿名内部类似乎是一个问题.
有没有更时髦的方式编写此代码

import org.apache.wicket.PageParameters
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.link.Link
import org.apache.wicket.markup.html.WebPage


/**
 * Homepage
 */
class HomePage extends WebPage {


    public HomePage(final PageParameters parameters) {

        // Add the simplest type of label
        add(new Label("message", "Wicket running!"));   
        def link1 = new ClickHandler("link1") //in java, defined inline
        add(link1);
    }   
}

class ClickHandler extends Link{

    ClickHandler(String id) {
        super(id);
    }

    void onClick(){println "Hi"}
}

解决方法:

我可能是错的,但这不是WickeBuilder试图解决的问题:

The Wicket Builder utility implements
a Groovy Builder for constructing
Wicket Component trees.

While using the builder makes building
Component trees easier and more clear
to the reader, the original driver was
the fact that Groovy does not allow
anonymous inner classes. Wicket
relies on overriding methods to
provide custom functionality for many
Component types. Groovy can be used
to code Wicket page classes, but each
class that is overridden needs a named
class definition. Possible, but
clunky.

The WicketBuilder simulates these
overrides with named Closures.
Closures are, essentially, portable
code blocks. Under the hood, the
builder creates dynamic class
overrides and runs the closures when
the named method is called.

[…]

标签:groovy,inner-classes,wicket,anonymous-class,java
来源: https://codeday.me/bug/20191210/2102060.html