POGO - A Groovy object whose class is written in Groovy. It extends java.lang.Object and implements the groovy.lang.GroovyObject interface by default.
Plain old Groovy object,用Groovy语言编写的原生类,继承自Java的java.lang.Object,实现了groovy.lang.GroovyObject接口。
GroovyObject接口实现的源代码:
执行结果:
结果表明,一个Groovy类实例试图调用一个POGO上没有定义的方法之时,POGO上被override的方法invokeMethod(在接口GroovyObject中声明)会自动被调用。
再看一个原生的Java类:
class JavaPerson{
String name;
public JavaPerson(String name) {
this.name = name;
}
public void hello() {
System.out.println("Hello: " + this.name);
}
}
JavaPerson jerry = new JavaPerson("Jerry");
jerry.hello();
try {
jerry.hello2();
} catch(Exception e) {
System.out.println("error: " + e.getMessage());
}
try代码块里会触发异常,因为JavaPerson类并没有定义名为hello2的方法:
error: No signature of method: jerry.JavaPerson.hello2() is applicable for argument types: () values: []
Possible solutions: hello(), sleep(long), sleep(long, groovy.lang.Closure), collect(), every()