对象私有与类私有

发布于 2024-08-16 04:54:48 字数 1358 浏览 9 评论 0原文

任何 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

有人总结了这些区别,在 C++ 中,“private”意味着“该类私有”,而在 Ruby 中,它的意思是“此实例私有”。这意味着,在 C++ 中,通过类 A 中的代码,您可以访问类型 A 的任何其他对象的任何私有方法。在 Ruby 中,您不能:您只能访问对象实例的私有方法,而不能访问任何其他对象的私有方法。对象实例(A 类)。

Is there a notion of object-private in any OOP language ?? I mean more restrictive than the classic private access ?

Private (or class-private) restricts the access to the class itself. Only methods that are part of the same class can access private members.

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 !!

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.

2 - Ruby thanks to Logan :

One person summed up the distinctions by saying that in C++, “private” means “private to this class”, while in Ruby it means “private to this instance”. What this means, in C++ from code in class A, you can access any private method for any other object of type A. In Ruby, you can not: you can only access private methods for your instance of object, and not for any other object instance (of class A).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

春花秋月 2024-08-23 04:54:48

在 ruby​​ 中,每个对象的私有是唯一的私有(您必须使用 protected 来获取类私有行为)。

例如 foo.rb:

 class A
    private
    def a=(x)
            @a=x
    end
    public
    def a
            @a
    end

    def b(c)
            c.a = 2
    end
 end

 a1 = A.new
 a2 = A.new
 a1.b(a2)

运行它,我们得到

 foo.rb:12:in `b': private method `a=' called for #<A:0xb7c9b6e0> (NoMethodError)
    from foo.rb:18

当然有解决这个问题的方法,但几乎总是有的。

In ruby, per-object private is the only private (you have to use protected to get class private behavior).

E.g. foo.rb:

 class A
    private
    def a=(x)
            @a=x
    end
    public
    def a
            @a
    end

    def b(c)
            c.a = 2
    end
 end

 a1 = A.new
 a2 = A.new
 a1.b(a2)

Running it, we get

 foo.rb:12:in `b': private method `a=' called for #<A:0xb7c9b6e0> (NoMethodError)
    from foo.rb:18

Of course there are ways around this, but there almost always are.

眼藏柔 2024-08-23 04:54:48

我认为您想要的功能可以通过不允许人员直接通信来实现。
为了以最小的努力实现这一点,您可以引入一个接口,该接口不会提供对您想要保密的内容的访问。

public interface IPerson
{
    void communicateFormally();
}

public class Person : IPerson 
{
    private String secret;
    public String othersSecret;

    public void snoop(IPerson p) {
      othersSecret = p.secret; //will be prohibited by the compiler
    }
    ...
}

现在,这可能会被丑陋的演员“黑客攻击”,但我认为这就是黑客攻击的问题。

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.

public interface IPerson
{
    void communicateFormally();
}

public class Person : IPerson 
{
    private String secret;
    public String othersSecret;

    public void snoop(IPerson p) {
      othersSecret = p.secret; //will be prohibited by the compiler
    }
    ...
}

Now, this could be "hacked" by an ugly cast, but I think that's the problem of the one hacking.

别闹i 2024-08-23 04:54:48

在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.

魔法唧唧 2024-08-23 04:54:48

我认为对于最常见的面向对象语言(例如 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.

往事风中埋 2024-08-23 04:54:48

是的,您可以在 Java 中创建包含该接口的其他实例无法看到的实例变量的对象。简单的例子:

class Secretive { }
Secretive s = new Secretive() {
    int unknowable = 42;
};
Secretive t = new Secretive() {
    String unfathomable = "banana";
};

Yes, you can create objects in Java containing instance variables that other instances of that interface cannot see. Trivial example:

class Secretive { }
Secretive s = new Secretive() {
    int unknowable = 42;
};
Secretive t = new Secretive() {
    String unfathomable = "banana";
};
假情假意假温柔 2024-08-23 04:54:48
    public class Person
    {
        private String privateSecret;
        public String PublicInformation;

        public void Snoop(Person p)
        {
            // will be allowed by the .NET compiler
            p.PublicInformation = p.privateSecret;
        }
    }

只需使用属性只读字段来加强您的安全性

您还可以使用 internal 访问器将您的类封装在组件中。

您还可以使用一些拒绝技术,例如此技术

    public class Person
    {
        private String privateSecret;
        public String PublicInformation;

        public void Snoop(Person p)
        {
            // will be allowed by the .NET compiler
            p.PublicInformation = p.privateSecret;
        }
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文