groovy 中的私有方法不是私有的
class A {
private def sayHello() {
println "Anish"
}
}
def a_obj = new A()
a_obj.sayHello()
输出:Anish
有什么方法可以保护 groovy 中的 sayHello()
还是我遗漏了一些东西?
class A {
private def sayHello() {
println "Anish"
}
}
def a_obj = new A()
a_obj.sayHello()
output : Anish
Is there any way to protect sayHello()
in groovy or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Groovy 问题跟踪系统中存在缺陷,并且该缺陷仍然存在。
There is defect on that in Groovy issue tracking system and that defect is still open.
搜索
[groovy] private
显示:groovy调用Java超类中的私有方法
“私有”是什么意思在 Groovy 中?
如何定义私有 getter 方法Groovy Bean?
目前尚不清楚这是一个错误还是设计使然,但在 Groovy 2.0 中将会再次审视它
Searching for
[groovy] private
reveals:groovy call private method in Java super class
What does 'private' mean in Groovy?
How to define private getter method in Groovy Bean?
It's not clear if it is a bug or by design, but it is going to get looked at again in Groovy 2.0
您可以使用闭包来实现类似的效果,基本上与使用 Javascript 进行信息隐藏的方式相同。
You can use closures to achieve a similar effect, basically the same way you would do information hiding with Javascript.
我认为这是 groovy 中的一个错误,已在 groovy++ 中修复。
https://issues.apache.org/jira/browse/GROOVY-1875
I think its a bug in groovy that is fixed in groovy++.
https://issues.apache.org/jira/browse/GROOVY-1875
正如其他帖子所提到的,这可能是 Groovy 中的一个错误。我一直坚持一个简单的约定,即在私有成员名称前加上前导下划线(类似于 Python)来表示它是私有的,这有助于我从客户端的角度理解我应该调用什么。
As other posts have mentioned, this may be a bug in Groovy. I've been sticking to a simple convention of prefixing private member names with a leading underscore (similar to Python) to denote that it's private which helps me understand from a client side perspective what I should be calling.