我如何在Groovy中获得类似匿名内部类的东西? [英] How do I get something like an anonymous inner class in Groovy?

查看:618
本文介绍了我如何在Groovy中获得类似匿名内部类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Groovy中定义一个匿名内部类?我看到了他们应该在Groovy 1.7中支持的引用,并且我使用了1.8。

  thread = process.consumeProcessOutput( 
新增Appendable(){
可附加追加(char c){
app1.append(c)
app2.append(c)
返回此
}

Appendable append(CharSequence csq){
app1.append(csq)
app2.append(csq)
return this
}

Appendable append(CharSequence csq,int start,int end){
app1.append(csq,start,end)
app2.append(csq,start,end)
返回这个
}
})

 抓住:groovy.lang.MissingMethodException:没有方法的签名:java.lang.UNIXProcess.consumeProcessOutput()适用于参数类型:(MyClass $ 1)values:[MyClass $ 1 @ 19c8ef56] 


解方案

这是一个棘手的情况,因为方法必须将对象本身作为 Appendable 返回,并且重载的方法名称不起作用以及groovy映射到接口铸造。最简单,最清晰的方法可能就是使用匿名内部类,就像在Java中一样。这需要一个合理的当前版本的groovy(1.7或更新,我认为):

  def testAppendable(Appendable appendable){
printlnappendable = $ appendable
appendable.append('a'as char)。
append('b'as char)。
append('c'as char)
}

testAppendable(new Appendable(){
Appendable append(char c){
printlngot $ c
this

可附加追加(CharSequence csq){
this
}
可附加追加(CharSequence csq,int start,int end) {
this
}
String toString(){inner class appendable}
});

另一种方法是使用 Expando 与关闭。这有点尴尬,因为每个方法名只有一个实现可以在构造函数中初始化。请注意,省略任何接口方法都会引发一个引发异常的默认实现。

  testAppendable(new Expando(
append :{char c - >
printlngot $ c
delegate as Appendable
},
toString:{ - >
expando appendable







$编辑:关于你的例子,我没有看看它为什么会失败。我的测试几乎完全相同,没有任何问题。 process.consumeProcessOutput 的签名是什么样的?另外,您可以通过运行 javap MyClass $ 1来仔细检查 MyClass $ 1 实现 Appendable code>。


How do I define an anonymous inner class in Groovy? I saw references that they were supposed to be supported in Groovy 1.7 and I'm using 1.8.

 thread = process.consumeProcessOutput(
   new Appendable() {
     Appendable append(char c) {
       app1.append(c)
       app2.append(c)
       return this
     }

     Appendable append(CharSequence csq) {
       app1.append(csq)
       app2.append(csq)
       return this
     }

     Appendable append(CharSequence csq, int start, int end) {
       app1.append(csq, start, end)
       app2.append(csq, start, end)
       return this
     }
   })

I get an exception with this code:

Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.UNIXProcess.consumeProcessOutput() is applicable for argument types: (MyClass$1) values: [MyClass$1@19c8ef56]

解决方案

This is a tricky case since the methods have to return the object itself as an Appendable, and has an overloaded method name that doesn't work well with groovy map to interface casting. The simplest, clearest way is probably to just use an anonymous inner class, as you would in Java. This requires a reasonably current version of groovy (1.7 or newer I think):

def testAppendable(Appendable appendable) {
    println "appendable = $appendable"
    appendable.append('a' as char).
               append('b' as char).
               append('c' as char)
}

testAppendable(new Appendable() {
    Appendable append(char c) {
        println "got $c"
        this
    }
    Appendable append(CharSequence csq) {
        this
    }
    Appendable append(CharSequence csq, int start, int end) {
        this
    }
    String toString() { "inner class appendable" }
});

Another alternative would be to use an Expando with closures. It's a bit awkward since only one implementation per method name can be initialized in the constructor. Note that any interface methods omitted are given a default implementation that throws an exception.

testAppendable(new Expando(
    append: { char c ->
        println "got $c"
        delegate as Appendable
    },
    toString: { ->
        "expando appendable"
    }
) as Appendable)

EDIT: Regarding your example, I don't see why it would fail. My test is almost identical and works without any issues. What does the signature of process.consumeProcessOutput look like? Also, you can double check that MyClass$1 implements Appendable by running javap MyClass$1.

这篇关于我如何在Groovy中获得类似匿名内部类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆