我需要以下类是可序列化的。
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;
}
}
截至目前,我无法将其作为具有输出流的对象发送。我已尝试实现可序列化,但没有成功。
最佳答案
实现 Serializable
将达到目的,但您必须使用 ObjectOutputStream 编写对象,而不仅仅是一个 OutputStream。
关于Java:如何使这个序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116147/