通过 ObjectOutputStream 发送自制对象时,该对象的服务器和客户端类是否必须相同?
例如,当某个 Action 因为需要其他几个只有在服务器上可用的类而只能在服务器上执行时,客户端上的类可以与服务器上的不同吗?
服务器类:
public class SomeAction implements Action, Serializable {
private static final long serialVersionUID = 1L; //Just some serialVersionUID
private String name;
public SomeAction(String name) {
//This property must be sent to the server
this.name = name;
}
@Override
public void performAction() {
System.out.println("New client connected");
Server.getConnections().add(1); //Increases the number of connections on the server. Of course, this is only available on the server.
//Do something with the client
. . .
}
}
客户端类:
public class SomeAction implements Action, Serializable {
private static final long serialVersionUID = 1L; //Just some serialVersionUID
private String name;
public SomeAction(String name) {
//This property must be sent to the server
this.name = name;
}
@Override
public void performAction() {
System.out.println("New client connected");
//The getConnections().add(1) wont work on the client.
//Do something with the client
. . .
}
}
现在客户端可以将它的类发送到服务器,然后服务器会performAction()
从它自己的类中调用方法吗?