Groovy 从重写的Java类调用方法。
我在从java类调用重写方法时遇到问题。 我有以下Java类:Groovy 从重写的Java类调用方法。,groovy,Groovy,我在从java类调用重写方法时遇到问题。 我有以下Java类: public class Base { int state = 0; public void called() { System.out.println("Hello, from called method: " + state); } public String getFirst() { return "From Base; }
public class Base
{
int state = 0;
public void called()
{
System.out.println("Hello, from called method: " + state);
}
public String getFirst()
{
return "From Base;
}
//
...
//
}
我使用groovy脚本重写getFirst()
,以便调用called()
如何实现此功能?您不能以这种方式使用代理魔法。。。在Maps声明时,它不知道它将成为Base的代理,因此它将抛出错误 为什么不按正常的方式做呢
def base = new Base() {
public String getFirst() {
called()
"from me"
}
}
嗯,行得通。谢谢,我在谷歌上搜索了一个多小时,发现groovy中的重写只通过闭包实现。
def base = new Base() {
public String getFirst() {
called()
"from me"
}
}