0

ObjectOutputStream を介して自作のオブジェクトを送信する場合、そのオブジェクトのサーバー クラスとクライアント クラスは同じである必要がありますか?

たとえば、サーバーでのみ使用可能な他のいくつかのクラスが必要なため、一部のアクションをサーバーでのみ実行できる場合、クライアント上のクラスはサーバー上のクラスと異なる可能性がありますか?

サーバークラス:

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()独自のクラスからメソッドを呼び出しますか?

4

1 に答える 1

1

クラスは異なる場合がありますが、それほど違いはありません。これらのクラス間の「互換性」に関していくつかの規則があります。

見る

http://docs.oracle.com/javase/7/docs/platform/serialization/spec/serialTOC.html

たとえば、クラスに新しいメソッドを追加し、serialVersionUID フィールドを同じ値に保ち、新しいメソッドを使用しないようにすることができます。もちろん、これは邪魔になりません。

アップデート:

より良いリンク:

http://web.archive.org/web/20051125013312/http://www.macchiato.com/columns/Durable4.html

于 2013-06-01T19:45:53.347 に答える