在Java中是否可以存储所需的对象并更改其值?
我正在研究rmi服务器客户端,我想将我的客户端存储到我服务器上的ArrayList
。在我尝试直接从服务器更改客户端值之前,这似乎很好。如果我使用的是C ++,我会声明一个指针。
我用look_up.LogIn(this);
服务器代码:
@Override
public void LogIn(Client temp) throws RemoteException {
for (Client arr : users) {
if (arr.equals(temp)) {
temp.setID(0);
}
}
// users.add(temp);
Random rand = new Random();
int randomNum = rand.nextInt((100 - 1) + 1) + 1;
boolean flag = false;
do {
for (Client arr : users) {
if (arr.getId() == randomNum) {
flag = true;
}
}
} while (flag);
temp.setID(randomNum);
System.out.println("Id we .. : " + temp.getId());
users.add(temp);
}
客户代码:
if (e.getSource() == logIn) {
try {
look_up.LogIn(this);
if (this.id != 0) {
JOptionPane.showMessageDialog(frame, "Loged in Succesfully");
logIn.setEnabled(false);
submit.setEnabled(true);
information.setText(look_up.check_Files());
tempValue = this.id;
System.out.println("Temp value afer login : " + tempValue);
} else {
JOptionPane.showMessageDialog(frame, "Sorry but somthing went wrong try to log in again...");
}
} catch (RemoteException ex) {
System.err.println("Object cant be send");
ex.printStackTrace();
}
}
这些代码行只有在我将LogIn
定义为int
并返回随机数值时才有效。当我试图直接更改它时,我收到了一个错误。
奇怪的是,当我设置temp.setId
它工作并打印正常但在客户端实际的实例ID仍为0.这怎么可能?
答案 0 :(得分:0)
我无法理解你的整个循环,但你似乎期望传递给服务器的参数能够在客户端神奇地改变它的值。这背叛了对正在发生的事情的完全误解。 Private Sub changeTextBox(tBox as TextBox)
'make changes here
End Sub
显然是Client
,因此它会按值传递给服务器。在服务器上对该对象所做的任何更改都将不在cliens中进行镜像。如果需要结果,则必须将其作为结果值从远程方法返回。