English 中文(简体)
object-private Vs class-private
原标题:

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

最佳回答

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.

问题回答

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.

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.

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.

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";
};
    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.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签