簡體   English   中英

Java:如何使這個Serializable?

[英]Java: How to make this Serializable?

我需要以下類是Serializable。

package helpers;

public class XY implements Comparable<XY>
{
    public int x;
    public int y;

    public XY (int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int compareTo( XY other ) 
    {
        String compare1 = this.x + "-" + this.y;
        String compare2 = other.x + "-" + other.y;

        return compare1.compareTo( compare2 );
    }

    public String toString()
    {
        return this.x + "-" + this.y;
    }

}

截至目前,我不能將它作為一個對象與outputstream一起發送..我試圖實現Serializable,但這不起作用。

實現Serializable 起到作用,但您必須使用ObjectOutputStream編寫對象,而不僅僅是OutputStream。

你只需要從java.io.Serializable繼承:

public class XY implements Comparable<XY>, java.io.Serializable

在此查看有關序列化的更多信息

請參閱Apache Commons Lang提供的SerializationUtils API

這樣更安全,更易於維護。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM