为什么Groovy Overridable中的(Java)公共方法?

时间:2017-11-23 16:14:42

标签: java groovy metaclass

public class JavaClass {
    public void echo() { 
        System.out.println("Echo'd from Java Class"); 
    }

    private void doSomethingElse() {
        System.out.println("Something else happening from Java Class");
    }
}

通常在Groovy中,(Java) private methods cannot be overridden。但是,似乎可以像下面那样覆盖公共方法:

JavaClass pojo = new JavaClass()
pojo.metaClass.echo = {
    println "Echo overridden!"
}

pojo.metaClass.doSomethingElse = {
    println "This won't be overridden"
}
println pojo.echo
println pojo.doSomethingElse

输出:

Echo overridden!
Something else happening from Java Class

如果无法覆盖私有方法,为什么允许使用公共方法?

0 个答案:

没有答案