Overriding private method with metaClass in Groovy

2019-07-02 16:22发布

问题:

This snippet used to work properly with Groovy 2.1.0:

class User {
    private String sayHello() {
        return "hello"
    }
}

assert new User().sayHello() == "hello"

User.metaClass.sayHello = {
    return "goodbye"
}

assert new User().sayHello() == "goodbye"

but it does not work anymore in Groovy 2.4.3. Does anybody know how to override the behaviour of a private method with Groovy (if possible)?