传了value给client,client又把value传给service,service中改变了value的值,client中value值也变了
程序输出:
a 123
b 456
public class Client {
public static void main(String[] args) {
Value aValue = new Value();
aValue.setUsernameString("a");
aValue.setPasswordString("123");
System.out.println(aValue.getUsernameString() + " " + aValue.getPasswordString());
Service service = new Service(aValue);
System.out.println(aValue.getUsernameString() + " " + aValue.getPasswordString());
}
}
package text_send_value;
public class Service {
Value aValue;
public Service(Value a) {
aValue = a;
aValue.setUsernameString("b");
aValue.setPasswordString("456");
//添加1处:aValue = null;
}
}
package text_send_value;
public class Value {
private String usernameString;
private String passwordString;
public String getUsernameString() {
return usernameString;
}
public void setUsernameString(String usernameString) {
this.usernameString = usernameString;
}
public String getPasswordString() {
return passwordString;
}
public void setPasswordString(String passwordString) {
this.passwordString = passwordString;
}
}
添加1处:赋值符号“=”会改变这引用的指向因为对象本身是不会变的,但对象自身的set方法改变自己内部值
程序输出
a 123
b 456
不会报错在 [添加1处]