Java:直接从同一个类的另一个实例访问私有字段

时间:2022-09-25 12:24:13

I'm writing a equals(Object obj) function for a class. I see that it is possible to access the private fields of obj from the caller. So instead of using a getter:

我在为一个类写一个等号(对象obj)函数。我看到可以从调用者访问obj的私有字段。所以不用getter:

Odp other = (Odp) obj;
if (! other.getCollection().contains(ftw)) {

}

I can just access the field directly:

我可以直接访问字段:

Odp other = (Odp) obj;
if (! other.collection.contains(ftw)) {

}

Is this bad practice?

这是不好的做法吗?

6 个解决方案

#1


6  

No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the internals of your class without having to change all the code that uses the class (that and to prevent the user of your class from e.g. setting a variable to a value that it's never supposed to have).

不,它不是。私有变量和方法的原因不是accessable从其他类允许您更改您的类的内部,而无需改变使用类的所有代码(和阻止你的类的用户如设置一个变量的值不应该)。

If you use private variables of other objects that doesn't hurt anything, because if you'd restructure your class's internals, you'd have to change the code inside the class anyway.

如果您使用其他对象的私有变量并没有伤害任何东西,因为如果您重构了类的内部结构,那么您将不得不更改类中的代码。

#2


9  

I tend to always use getters, because sometimes a getter isn't just "return(foo)". Sometimes they initialize things if they're null, or have some debug logging in them, or validate the current state in some way. It's more consistent.

我倾向于总是使用getter,因为有时getter不是“返回(foo)”。有时,如果它们是空的,它们会初始化一些东西,或者在其中进行一些调试日志记录,或者以某种方式验证当前状态。更多的是一致的。

#3


5  

I dont think this is bad practice, but a feature of the language. It not only allows you to test equals the way you do, but it is also useful in a Prototype pattern for object creation.

我认为这不是坏习惯,而是语言的一个特点。它不仅允许您以相同的方式进行测试,而且在创建对象的原型模式中也很有用。

#4


1  

That is fine and completely normal. It is a little bit odd to think that this can fiddle with the private fields of other, but it's okay because there's no way anything bad can happen as far as some third party being able to muck with an Odp object's internals. Any method of the Odp class can modify any private members of any Odp object, even non-this ones, but that's fine since any such methods can obviously be trusted!

这是很正常的。这有点奇怪,因为它可以摆弄其他的私有字段,但是没关系,因为没有任何不好的事情可以发生,就像一些第三方能够用Odp对象的内部来处理一样。Odp类的任何方法都可以修改任何Odp对象的任何私有成员,即使不是这个对象,但这是可以的,因为任何这样的方法都可以被信任!

#5


1  

This is a duplication of

这是重复的

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

为什么我可以在equals(object o)方法中直接访问“其他”对象的私有变量

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

该类的任何实例都可以访问私有数据,即使类A的一个实例正在访问另一个实例的私有成员。重要的是要记住,访问修饰符(Private、protected、public)控制类访问,而不是实例访问

#6


0  

Using private member for entity class may caused proxy class work incorrectly. Imagine hibernate create class by lazy query. If you check member variable , it return null. But if you call get() , it will fetch data from database and initialise the field.

为实体类使用私有成员可能导致代理类工作不正确。想像hibernate通过惰性查询创建类。如果检查成员变量,它返回null。但是如果您调用get(),它将从数据库获取数据并初始化字段。

#1


6  

No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the internals of your class without having to change all the code that uses the class (that and to prevent the user of your class from e.g. setting a variable to a value that it's never supposed to have).

不,它不是。私有变量和方法的原因不是accessable从其他类允许您更改您的类的内部,而无需改变使用类的所有代码(和阻止你的类的用户如设置一个变量的值不应该)。

If you use private variables of other objects that doesn't hurt anything, because if you'd restructure your class's internals, you'd have to change the code inside the class anyway.

如果您使用其他对象的私有变量并没有伤害任何东西,因为如果您重构了类的内部结构,那么您将不得不更改类中的代码。

#2


9  

I tend to always use getters, because sometimes a getter isn't just "return(foo)". Sometimes they initialize things if they're null, or have some debug logging in them, or validate the current state in some way. It's more consistent.

我倾向于总是使用getter,因为有时getter不是“返回(foo)”。有时,如果它们是空的,它们会初始化一些东西,或者在其中进行一些调试日志记录,或者以某种方式验证当前状态。更多的是一致的。

#3


5  

I dont think this is bad practice, but a feature of the language. It not only allows you to test equals the way you do, but it is also useful in a Prototype pattern for object creation.

我认为这不是坏习惯,而是语言的一个特点。它不仅允许您以相同的方式进行测试,而且在创建对象的原型模式中也很有用。

#4


1  

That is fine and completely normal. It is a little bit odd to think that this can fiddle with the private fields of other, but it's okay because there's no way anything bad can happen as far as some third party being able to muck with an Odp object's internals. Any method of the Odp class can modify any private members of any Odp object, even non-this ones, but that's fine since any such methods can obviously be trusted!

这是很正常的。这有点奇怪,因为它可以摆弄其他的私有字段,但是没关系,因为没有任何不好的事情可以发生,就像一些第三方能够用Odp对象的内部来处理一样。Odp类的任何方法都可以修改任何Odp对象的任何私有成员,即使不是这个对象,但这是可以的,因为任何这样的方法都可以被信任!

#5


1  

This is a duplication of

这是重复的

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

为什么我可以在equals(object o)方法中直接访问“其他”对象的私有变量

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

该类的任何实例都可以访问私有数据,即使类A的一个实例正在访问另一个实例的私有成员。重要的是要记住,访问修饰符(Private、protected、public)控制类访问,而不是实例访问

#6


0  

Using private member for entity class may caused proxy class work incorrectly. Imagine hibernate create class by lazy query. If you check member variable , it return null. But if you call get() , it will fetch data from database and initialise the field.

为实体类使用私有成员可能导致代理类工作不正确。想像hibernate通过惰性查询创建类。如果检查成员变量,它返回null。但是如果您调用get(),它将从数据库获取数据并初始化字段。