Overriding private method with metaClass in Groovy

扶醉桌前 提交于 2019-12-23 18:17:47

问题


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)?

来源:https://stackoverflow.com/questions/31938551/overriding-private-method-with-metaclass-in-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!