gpt4 book ai didi

java - 序列化Point,PointF对象

转载 作者:行者123 更新时间:2023-12-03 12:00:37 25 4
gpt4 key购买 nike

我是序列化概念的新手,由于要通过套接字连接发送PointF类(我正在使用ObjectOutputStream和ObjectInputStream),因此我尝试对PointF类进行序列化。我的问题是在发送时,无论我在发送时在PointF对象中具有什么值,在接收到默认值之后。

例如,如果我发送了pointF(1.0,4.0),我会得到(0.0,0.0)。

以下是我的隐含可序列化的代码。

public class MyPointF extends PointF implements Serializable {

/**
*
*/
private static final long serialVersionUID = -455530706921004893L;

public MyPointF() {
super();
}

public MyPointF(float x, float y) {
super(x, y);
}
}

有人可以指出这个问题吗?
另外,在搜索了一点之后,我发现这件事也发生在android.canvas.Path类上。请纠正我我错了的地方。

最佳答案

您的父类(super class)PointF不可序列化。这意味着以下情况适用:

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.



另请: http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

您将需要查看 readObjectwriteObject:

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

 private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException;


另请参见此处: Java Serialization with non serializable parts,以获取更多提示和技巧。

关于java - 序列化Point,PointF对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21179794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com