java - groovy中的匿名内部类

标签 java groovy wicket inner-classes anonymous-class

我正在研究 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.

[...]

关于java - groovy中的匿名内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1442019/

相关文章:

wicket - 通过 Wicket 中的 URL 使操作可用

java - 匹配一组字符串的正则表达式

java - Photoshop 图片在 Android 应用程序开发中无法运行(使用 Eclipse)

eclipse - 使用 gradle 添加 eclipse 项目构面

java - FormComponents 有共同的 isVisible 方法

javascript - Wicket 口和 AJAX : Upload file to server and show it on page

java - 为什么 JVM 无法检测到一些无效路径

java - 对于轻松的 https 调用,如何接受所​​有证书

sql - 来自 grails 项目的 hibernate AssertionFailure

grails - 检查父类(super class)字段上是否存在注释