Java:getter方法与公共实例变量:性能和内存 [英] Java: getter method vs. public instance variable: performance and memory

查看:112
本文介绍了Java:getter方法与公共实例变量:性能和内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于noob问题抱歉。通过引用和值传递很难!

Sorry for the noob questions. Passing by reference vs. value is hard!

所以我有一个具有相当大的数据结构的类 - 多维数组。我需要从另一个类访问这些数组。我可以将数组公开并执行经典的objectWithStructures.structureOne。或者,我可以做getters:添加一个像public int [] [] [] getStructureOne()的方法。

So I have a class with pretty big data structures-- multidimensional arrays. I need to access these arrays from another class. I could just make the arrays public and do the classic objectWithStructures.structureOne. Or, I could do getters: adding a method like public int[][][] getStructureOne().

是否有一个getter复制多维数组?或者它通过引用传递它,你只是不能改变引用的对象?

Does having a getter make a copy of the multidimensional array? Or does it pass it by reference and you just can't alter the object referenced?

我担心内存和性能。但是将数据结构公开,如果它不会导致副本更快,那么看起来就像编写糟糕的编码一样。

I'm worried about memory and performance. But making the data structures public, while faster if it doesn't cause copies to be made, seems like poor coding practice.

ADDENDUM:所以当我返回一个引用时使用getter方法的对象(例如数组),是否可以使用getter方法编辑该对象?或者它是否以某种方式锁定进行编辑,以便只有它所在的类可以改变该对象?

ADDENDUM: So when I return a reference to an object (e.g. an array) using a getter method, can that object be edited by whoever uses the getter method? Or is it somehow "locked" for editing so that only the class it's in can alter that object?

推荐答案


通过引用传递与值相比很难

Passing by reference vs. value is hard

在Java中,所有内容都按值传递。原始类型通过其值传递,复杂类型通过其引用的值传递。数组不是原始的,因此它们的引用会被传递。

In Java everything is passed by value. Primitive types are passed by their value, Complex types are passed by the value of their reference. Arrays are not primitive so their reference is passed around.


我可以将数组公开...或者,我可以做getters

I could just make the arrays public ... Or, I could do getters

如果它们是 public 那么其他类可以更改对象的数组引用包含自己。如果有getter返回对数组的引用,则调用者可以更改数组的内容。两者都很糟糕。这也回答了你的附录问题。

If they are public then other classes can change the array reference that your object contains itself. If there are getters that return reference to the array then callers can change the contents of the array. Both are pretty bad. This answers your addendum question as well.

我认为选项如下:


  • @Bohemian建议使用单个细胞的吸气剂。如果需要对数组对象进行任何锁定,则可能会有额外的panalties

  • Use getters for individual cells as @Bohemian suggests. If there is any locking of the array object is required there may be additional panalties here

返回对数组的引用并信任您的调用者不要搞砸它。可以通过在编译时检查访问来实现额外的chekcs

Return reference to the array and trust your callers to not mess it up. Additional chekcs can be implemented by checking access at compile time

在对象内维护两个的数组副本。一个是在吸气剂中返回,另一个是为了真正的工作。您可以记录更改数组没有任何效果,并断言从getter返回时它们的内容相同。

Maintain two copies of the array inside the object. One to return in the getter and the other for the real work. You can document that changing the array has no effect and assert that their contents are same when returning from getter.

这篇关于Java:getter方法与公共实例变量:性能和内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆