在java中更改对象的值
我正在编写一个简单的应用程序,如bellow:在java中更改对象的值,java,Java,我正在编写一个简单的应用程序,如bellow: public class Test1 { String name; public Test1(String name) { this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { retu
public class Test1 {
String name;
public Test1(String name) {
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
及
公共类Test2{
/**
*@param args
*/
公共静态void main(字符串[]args){
对于(int i=0;i6)中断;
}
Test2 Test2=新的Test2();
Test1 Test1=新的Test1(“Test1”);
test2.doTest(test1);
System.out.println(test1);
}
公共void doTest(Test1 Test1)
{
test1=新的test1(“Test2”);
}
}
当我将test1对象引用传递给doTest并为该对象重新分配一个新值时,我相信avobe程序的输出将是Test2。但它只给出Test1。在doPost内部创建的新对象似乎是该方法的本地对象,不会影响外部对象。但在这里,我将引用传递给对象,为什么对象的值仍然不会更改为Test2?它不会更改,因为您正在更改方法内部的引用,而不是对象的状态 方法和构造函数参数总是按值传递,对于引用变量,该值就是引用本身。如果在方法内部更改引用赋值,它所做的只是将引用重新赋值给技术上是局部变量的参数,并且不会影响原始变量所指向的引用 如果改为调用
test1.setName(“Test2”)
:
结果将完全不同,因为现在您将更改引用对象的状态。不会更改,因为您正在更改方法内部的引用,而不是对象的状态 方法和构造函数参数总是按值传递,对于引用变量,该值就是引用本身。如果在方法内部更改引用赋值,它所做的只是将引用重新赋值给技术上是局部变量的参数,并且不会影响原始变量所指向的引用 如果改为调用
test1.setName(“Test2”)
:
结果将完全不同,因为现在您将更改引用对象的状态。您无法更改对方法范围内第一个对象的引用。表示指定给
doTest
的对象将仅用作输入
您可以做的是将新对象作为方法的结果返回,并在外部使用新对象
比如:
public Test1 doTest(Test1 test1) {
// do what you need to do with test1
// ...
return new Test1("Test2");
}
test1 = test2.doTest(test1); // here you are overwriting the reference
// with the version returned from the
// method invocation
在外面你会有这样的东西:
public Test1 doTest(Test1 test1) {
// do what you need to do with test1
// ...
return new Test1("Test2");
}
test1 = test2.doTest(test1); // here you are overwriting the reference
// with the version returned from the
// method invocation
这种方法在许多java API中都可以看到,一个例子是将一个列表作为参数并给出另一个作为结果的方法。您不能更改对方法范围内第一个对象的引用。表示指定给
doTest
的对象将仅用作输入
您可以做的是将新对象作为方法的结果返回,并在外部使用新对象
比如:
public Test1 doTest(Test1 test1) {
// do what you need to do with test1
// ...
return new Test1("Test2");
}
test1 = test2.doTest(test1); // here you are overwriting the reference
// with the version returned from the
// method invocation
在外面你会有这样的东西:
public Test1 doTest(Test1 test1) {
// do what you need to do with test1
// ...
return new Test1("Test2");
}
test1 = test2.doTest(test1); // here you are overwriting the reference
// with the version returned from the
// method invocation
这种方法可以在许多java API中看到,一个例子是将一个列表作为参数并给出另一个作为结果的方法。为了理解,让我们将doTest()的参数重命名为
obj
public void doTest(Test1 obj) {
obj = new Test1("Test2");
}
- 调用doTest时:
(test1)test2.doTest(test1)代码>您正在传递引用的副本
- 现在
和obj
都用test1
name='test1'引用obj
- 在
doTest()。但是
仍然指向main中带有test1
的对象name=test1
- 因此,当调用toString()时,它会打印
Test1
test1
总是使用name=test1引用obj
为了理解,让我们将doTest()的参数重命名为
obj
public void doTest(Test1 obj) {
obj = new Test1("Test2");
}
- 调用doTest时:
(test1)test2.doTest(test1)代码>您正在传递引用的副本
- 现在
和obj
都用test1
name='test1'引用obj
- 在
doTest()。但是
仍然指向main中带有test1
的对象name=test1
- 因此,当调用toString()时,它会打印
Test1
test1
总是使用name=test1引用obj
一种方法不应该这样使用。要使用它,您应该将sysout移到方法(doTest)中。然后,当您需要该实例时,使用该方法内部所需的代码调用该方法。方法不适合这样使用。要使用它,您应该将sysout移到方法(doTest)中。然后,当您需要该实例时,使用方法内部所需的代码调用该方法