【发布时间】:2013-06-02 20:34:37
【问题描述】:
当通过 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()方法吗?
【问题讨论】:
标签: java java-io objectoutputstream