Java:如何使这个Serializable? [英] Java: How to make this Serializable?

查看:101
本文介绍了Java:如何使这个Serializable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下类是Serializable。

I need the following class to be 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;
    }

}

截至目前,我可以' t将它作为一个带有outputstream的对象发送。我试图实现Serializable,但是没有用。

As of now, I can't send it as an object with outputstream..I´ve tried to implement Serializable, but that didn't work.

推荐答案

实现 Serializable 执行操作,但您必须使用 ObjectOutputStream ,而不仅仅是一个OutputStream。

Implementing Serializable will do the trick, but you must write the object with an ObjectOutputStream, not just an OutputStream.

这篇关于Java:如何使这个Serializable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆