对象私有与类私有
任何 OOP 语言中是否都有对象私有的概念?我的意思是比经典的私人访问限制更多?
私有(或类私有)限制对类本身的访问。只有属于同一类的方法才能访问私有成员。
object-private :限制对对象本身的访问。只有可以访问成员的方法对象,并且无法编写:
public class Person {
private String secret;
public String othersSecret;
public void snoop(Person p) {
othersSecret = p.secret; //will be prohibited by the compiler
}
编辑:
如果存在,您能给我一些例子吗...如果不存在,您认为拥有这种功能很有趣吗? ?是否可以用其他 OOP 语言来模拟它?
编辑2: 谢谢你们,所有的答案都非常有启发性......
到目前为止,临时结论:
instance-private 概念存在于 2 种语言中:
1 - Smalltalk 经过几个小时的谷歌搜索:) 我找到了这个概念背后的语言!
对象所持有的状态始终对该对象而言是私有的。其他对象只能通过向该对象发送请求(消息)来查询或更改该状态。
2 - Ruby 感谢 Logan :
Is there a notion of object-private in any OOP language ?? I mean more restrictive than the classic private access ?
object-private : restricts the access to the object itself. Only methods objects that can access members and it will be impossible to write :
public class Person {
private String secret;
public String othersSecret;
public void snoop(Person p) {
othersSecret = p.secret; //will be prohibited by the compiler
}
EDIT :
If it exist can you give me some examples ... if not do you think it's interesting to have this kind of feature ?? and is it possible to simulate it in others OOP languages ??
EDIT 2 :
Thanks you guys, all the answers were very instructive ...
Until now, the temporary conclusion :
The instance-private notion exists in 2 languages :
1 - Smalltalk after hours of googling :) I found the language behind this concept !!
2 - Ruby thanks to Logan :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 ruby 中,每个对象的私有是唯一的私有(您必须使用
protected
来获取类私有行为)。例如 foo.rb:
运行它,我们得到
当然有解决这个问题的方法,但几乎总是有的。
In ruby, per-object private is the only private (you have to use
protected
to get class private behavior).E.g. foo.rb:
Running it, we get
Of course there are ways around this, but there almost always are.
我认为您想要的功能可以通过不允许人员直接通信来实现。
为了以最小的努力实现这一点,您可以引入一个接口,该接口不会提供对您想要保密的内容的访问。
现在,这可能会被丑陋的演员“黑客攻击”,但我认为这就是黑客攻击的问题。
I think the feature you want could be implemented by, figuratively, not allowing Persons to communicate directly.
To achieve this with minimal effort you can introduce an interface, which would not provide access to things you want to make secret.
Now, this could be "hacked" by an ugly cast, but I think that's the problem of the one hacking.
经过几个小时的谷歌搜索:)我找到了这个概念背后的语言:Smalltalk
状态对象持有的对象始终是该对象私有的。其他对象只能通过向该对象发送请求(消息)来查询或更改该状态。
After hours of googling :) I found the language behind this concept : Smalltalk
The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so.
在Java中,就像你正在写的那样,“私有”意味着类私有。没有办法强制对象私有模式。原因是“私有”是一种强制封装的方式,而不是安全。
In Java, which is what it looks like you are writing, "private" means class-private. There is no way to force object-private mode. The reason for this is that "private" is a way of enforcing encapsulation, not security.
我认为对于最常见的面向对象语言(例如 C#、C++、Python、Java、Objective-C),类与对象
private
之间不存在这种区别……公平地说,我可以”不记得有一种语言实际上具有此功能。I don't think this kind of distinction of class vs object
private
exists for the most common languages OO such as C#, C++, Python, Java, Objective-C ... To be fair I can't remember of a language that actually has this feature.是的,您可以在 Java 中创建包含该接口的其他实例无法看到的实例变量的对象。简单的例子:
Yes, you can create objects in Java containing instance variables that other instances of that interface cannot see. Trivial example:
只需使用属性或只读字段来加强您的安全性。
您还可以使用 internal 访问器将您的类封装在组件中。
您还可以使用一些拒绝技术,例如此技术。
just use properties, or readonly fields to enforce your security.
You can use also internal accessor to incapsulate your class in a assambley.
You can also use some Deny techniques like this one.