C++-like friend class mechanism in Java

江枫思渺然 提交于 2019-12-01 02:37:20

问题


Do you know how can I make an object changeable only inside a special class? In this example I want the object PrivateObject to be only changable (incrementable) inside the Box class, nowhere else. Is there a way to achieve this?

public class Box { 

    private PrivateObject prv;

    public void setPrivateObject(PrivateObject p){
        prv = p;
    }

    public void changeValue(){
        prv.increment();

    }

}


public class PrivateObject {
    private value;
    public increment(){
        value++;
    }

}

PrivateObject priv = new PrivateObject ();
Box box = new Box();
box.setPPrivateObject(priv);
box.changevalue();
priv.increment(); // I don't want it to be changeable outside the Box class!

In C++, I would make all the PrivateObject properties and methods private, and the declare the Box class as a friend for the PrivateObject class.


回答1:


If PrivateObject is strongly related to Box why not make it an inner class inside Box?

class Box { 

    public static class PrivateObject {
       private value;

       private increment(){
         value++;
       }
    }

    private PrivateObject prv;

    public void setPrivateObject(PrivateObject p){
        prv = p;
    }

    public void changeValue(){
        prv.increment();
    }       
}

Now you cannot call increment from outside Box:

public static void main(String args[]) {
    Box.PrivateObject obj = new Box.PrivateObject();
    Box b = new Box();
    b.setPrivateObject(obj);
    obj.increment(); // not visible     
}



回答2:


The closest would be to make PrivateObject an inner class of Box and make increment private. That way, the method will be accessible only within the Box class.

public class Box {

    private PrivateObject prv;

    public void setPrivateObject(PrivateObject p) {
        prv = p;
    }

    public void changeValue() {
        prv.increment();

    }

    public static class PrivateObject {

        private int value;

        private void increment() {
            value++;
        }
    }
}

If you don't want to do that, the next option would be to make increment package private and locate the 2 classes in the same package. That way, only classes within that package will have access to that method. But that might include classes other than Box.




回答3:


Java does not have any language feature equivalent of C++'s friend keyword. However , there are number of application-level alternatives :

  1. Declare the increment() method of the PrivateObject class as package- private and define Box in the same package.

OR

2.Let the calling code pass a token into increment() - inside this method check if this token is indeed coming from Box class and allow or disallow.

The idea is to keep the coupling between PrivateObject and Box class manageable.




回答4:


In Java, you would create a group of "friends" by putting multiple classes in the same package. Then, use default scope (no access specifier) on the classes, methods and fields you want to restrict access to.



来源:https://stackoverflow.com/questions/10848333/c-like-friend-class-mechanism-in-java

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