java 私有变量的改变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30107772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
changing of private variables
提问by Wim Van Laer
Since a few months I'm having a Java course and I'm trying to make a simple version of a pokemon game. Until now everything went pretty well but now I'm having troubles.
几个月以来,我正在学习 Java 课程,并且正在尝试制作一个简单版本的口袋妖怪游戏。到目前为止,一切都很顺利,但现在我遇到了麻烦。
I have my map with obstacles stored in a class in an Array (private) and use a get method to use the Array in other classes. Somehow these other classes change the Array in the first class.
我有我的地图,障碍物存储在数组(私有)的类中,并使用 get 方法在其他类中使用数组。这些其他类以某种方式更改了第一个类中的 Array。
How is this possible?
这怎么可能?
采纳答案by Jesper
Without seeing your code, it's hard to say what exactly is happening in your program. Maybe something like the following is happening.
如果没有看到你的代码,很难说你的程序到底发生了什么。也许正在发生类似以下的事情。
Maybe you have a class that has a method that returns a mutable, private member. When you do this, and some other class calls this method, it will be able to change the content of the object that the private member variable refers to. Here is an example:
也许你有一个类,它有一个返回可变私有成员的方法。当您这样做时,并且其他一些类调用此方法,它将能够更改私有成员变量所引用的对象的内容。下面是一个例子:
import java.util.Arrays;
class Example {
// Private member variable
private int[] data = { 1, 2, 3 };
// Method that returns the private member variable
public int[] getData() {
return data;
}
public void showData() {
System.out.println(Arrays.toString(data));
}
}
public class Main {
public static void main(String[] args) {
Example example = new Example();
// Prints: [1, 2, 3]
example.showData();
// Get the array
int[] x = example.getData();
// We can modify the array here!
x[0] = 4;
// Prints: [4, 2, 3]
example.showData();
}
}
This is because objects are arrays, and variables such as data
and x
are referencesto the same array object - there's only one array and data
and x
both refer to that one array. If you modify the array, you'll see the changes through both variables.
这是因为对象是数组,如变量data
和x
是引用相同的数组对象-只有一个阵列和data
与x
这两个指的是一个数组。如果修改数组,您将通过两个变量看到更改。
If you want to avoid this, then you shouldn't expose the array by returning it directly in the getData()
method in class Example
. You should make a copy of the array:
如果你想避免这种情况,那么你不应该通过直接getData()
在 class的方法中返回它来公开数组Example
。您应该制作数组的副本:
public int[] getData() {
// Return a copy of the array
return Arrays.copyOf(data, data.length);
}
回答by Eranda
Private does not mean it can't be mutated. Private means the reference of that object cannot access directly using the parent object. If you have a getter and which return object reference then any object which has that instance can mutate it.
私有并不意味着它不能被变异。私有意味着该对象的引用不能直接使用父对象访问。如果你有一个 getter 并且它返回对象引用,那么任何具有该实例的对象都可以改变它。
回答by Thusitha Thilina Dayaratne
private
doesn't mean that you can't change the value of particular thing. when you make a variable private it can't be directly access outside the class. This means that you are no longer able to access a private variable in a class like follows
private
并不意味着你不能改变特定事物的价值。当您将变量设为私有时,它不能在类外直接访问。这意味着您不再能够访问类中的私有变量,如下所示
class A{
private int x = 10;
}
class B{
public void m(){
A a = new A();
a.x = 20; // This is a compile error. Because x is not visible to outside of class A
}
}
Though still you are able to access the variable through a public method. That is what we normally call as encapsulation. e.g.
尽管您仍然可以通过公共方法访问该变量。这就是我们通常所说的封装。例如
class A{
private int x = 10;
public int getX(){
return x;
}
public void setX(int val){
x = val;
}
}
class B{
public void m(){
A a = new A();
a.setX(20);
}
}
回答by Code Knight
Yes. By using reflection, you can access your private field without giving reference methods.
For example:
是的。通过使用反射,您可以在不提供参考方法的情况下访问您的私有字段。
例如:
Field field = YourClass.class.getDeclaredField("fieldName");
field.setAccessible(true); // Force to access the field
// Set value
field.set(yourClassInstance, "Something");
// Get value
Object value = field.get(yourClassInstance);