Why can I access my private variables of the “other” object directly, in my equals(Object o) method

你说的曾经没有我的故事 提交于 2019-11-27 12:21:08

Private data is accessible by any instance of that class, even if one instance of class A is accessing the private members of another instance of A. It's important to remember that that access modifiers (private, protected, public) are controlling class access, not instance access.

The probable answer is that the designer of the visibility model considers that any developer working in a class has to master the implementation of the whole class.

But this is a bad idea. This encourages bad practice. A developer accessing a field of Person, in the class Person, does not have to know the implementation of the whole class. The good practice is to use the accessor, without having to know what operations the accessor does.

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