Java:如何使其可序列化?
我需要以下类可以序列化Java:如何使其可序列化?,java,serialization,outputstream,Java,Serialization,Outputstream,我需要以下类可以序列化 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 ) {
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;
}
}
包助手;
公共类XY实现了可比性
{
公共int x;
公共智力;
公共XY(整数x,整数y)
{
这个.x=x;
这个。y=y;
}
公共整数比较(XY其他)
{
字符串compare1=this.x+“-”+this.y;
字符串compare2=other.x+“-”+other.y;
返回compare1.compareTo(compare2);
}
公共字符串toString()
{
返回此.x+“-”+此.y;
}
}
到目前为止,我无法使用outputstream将其作为对象发送。我尝试实现Serializable,但没有成功。请参阅Apache Commons Lang提供的
这样更安全,维护更方便。:) 您只需要从
java.io.Serializable
继承:
公共类XY实现了可比较的、java.io.Serializable
查看有关序列化的更多信息。实现
Serializable
可以做到这一点,但您必须使用一个而不仅仅是一个OutputStream来编写对象。“我只是尝试实现Serializable,但它没有做到这一点!”您到底尝试了什么。。?你到底被困在哪里了?这应该很容易…发布你的序列化代码。