[英]Changing a java object outside its class
Here's my question, how can I change an object outside of it's class, so that it maintains the changes made in the outside class? 这是我的问题,我如何才能在类的外部更改对象,以使其保持在外部类中所做的更改?
Here's an example of the code: 这是代码示例:
Main class: 主班:
public class Main {
public static void main(String[] args)
{
Variable var = new Variable(1,2,3);
Change.changeVar(var);
System.out.println("" + var.geta() + "" + var.getb() + "" + var.getc());
}
}
Variable class: 变量类:
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
} }
Change class: 变更类别:
public class Change {
public static void changeVar(Variable var)
{
Variable var2 = new Variable(4,5,6);
var = var2;
}
} }
In your example, no. 在您的示例中,没有。 When changeVar() exits, the parameter var is discarded, and the var in your main() method retains its original value.
当changeVar()退出时,参数var将被丢弃,并且main()方法中的var保留其原始值。 Read up on pass by reference .
通过参考阅读。
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
// depending on your use case, setters might be more appropriate
// it depends on how you want to control the changing of the vars
public void update(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
public class Change {
public static void changeVar(Variable var)
{
var.update(4,5,6);
}
}
Doing it that way? 那样做吗? You can't.
你不能
You're passing a reference to the instance. 您正在传递对该实例的引用。 However, inside the function, you use a new reference.
但是,在函数内部,您使用了新引用。 Assigning to the new reference does not affect others.
分配给新参考不会影响其他参考。
You cannot do it in a way that you described, because in Java variables are passed by values. 您不能以您描述的方式进行操作,因为在Java中,变量是通过值传递的。 However you can achieve the desired effect in a different way:
但是,您可以通过其他方式实现所需的效果:
public class Variable {
private int a;
private int b;
private int c;
public Variable(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public int geta()
{
return this.a;
}
public int getb()
{
return this.b;
}
public int getc()
{
return this.c;
}
public void seta(int a) { this.a = a; }
public void setb(int b) { this.a = b; }
public void setc(int c) { this.a = c; }
}
public class Change {
public static void changeVar(Variable var)
{
var.seta(4);
var.setb(5);
var.setc(6);
}
}
You need to provide setter methods and call them on the original object: 您需要提供setter方法并在原始对象上调用它们:
public void seta(int newa) { this.a = newa; }
Then you would say 那你会说
public static void changeVar(Variable var)
{
var.seta(4);
//etc
}
You are merely repointing the local variable reference var
to point to your new instance var2
. 您只是在重新指向局部变量引用
var
指向新实例var2
。 It has no effect on the value of the original instance passed into the method. 它对传递给该方法的原始实例的值没有影响。
public static void changeVar(Variable var)
{
Variable var2 = new Variable(4,5,6);
var = var2;
}
first, u can write some setter methods in Variable class, then you can call these setter methods in the above code, like var.setA(4)
... and so on. 首先,您可以在Variable类中编写一些setter方法,然后可以在上面的代码中调用这些setter方法,例如
var.setA(4)
...等等。 enter code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.