gpt4 book ai didi

java - NotSerializedException 实现 Serialized 的序列化类

转载 作者:行者123 更新时间:2023-11-30 07:36:28 24 4
gpt4 key购买 nike

我在使用 ObjectOutputStream#writeObject 方法序列化此类时遇到问题。

这是我的类(class):

public class InteractedObject implements Serializable{

private static final long serialVersionUID = 537798409913313981L;

protected int id;
protected WorldTile tile;
protected int option;

private long interactionTime;

public InteractedObject(int id, WorldTile tile, int option){
this.id = id;
this.tile = tile;
this.option = option;
}

public long getInteractionTime(){
return interactionTime;
}

public void setInteractionTime(long interactionTime){
this.interactionTime = interactionTime;
}

public boolean isEqualTo(Object o){
if(this == o)
return true;
InteractedObject obj = o instanceof InteractedObject ? (InteractedObject)o : null;
if(obj == null)
return false;
return obj.id == id && obj.tile.equals(tile) && obj.option == option;
}
}

WorldTile 类确实实现了 Serialized 接口(interface),并且仅使用类型为 shortbyte 的字段。我不明白为什么这会为我的类“a.rodit.rs.InteractedObject”(最上面的一个)抛出 NotSerializedException

任何有关为什么会发生这种情况的帮助或指导将不胜感激。

编辑:

我的 WorldTile 类如下:

public class WorldTile implements Serializable {

private static final long serialVersionUID = -6567346497259686765L;

private short x, y;
private byte plane;

public WorldTile(){
this(0, 0, 0);
}

public WorldTile(int x, int y, int plane) {
this.x = (short) x;
this.y = (short) y;
this.plane = (byte) plane;
}
public final WorldTile getLocation() {
WorldTile tile = new WorldTile(x, y, plane);
return tile;
}
public WorldTile(WorldTile tile) {
this.x = tile.x;
this.y = tile.y;
this.plane = tile.plane;
}

public WorldTile(WorldTile tile, int randomize) {
this.x = (short) (tile.x + Utils.getRandom(randomize * 2) - randomize);
this.y = (short) (tile.y + Utils.getRandom(randomize * 2) - randomize);
this.plane = tile.plane;
}

public void moveLocation(int xOffset, int yOffset, int planeOffset) {
x += xOffset;
y += yOffset;
plane += planeOffset;
}

public final void setLocation(WorldTile tile) {
setLocation(tile.x, tile.y, tile.plane);
}

public final void setLocation(int x, int y, int plane) {
this.x = (short) x;
this.y = (short) y;
this.plane = (byte) plane;
}

public int getX() {
return x;
}

public int getXInRegion() {
return x & 0x3F;
}

public int getYInRegion() {
return y & 0x3F;
}

public int getY() {
return y;
}

public int getPlane() {
if (plane > 3)
return 3;
return plane;
}

public int getChunkX() {
return (x >> 3);
}

public int getChunkY() {
return (y >> 3);
}

public int getRegionX() {
return (x >> 6);
}

public int getRegionY() {
return (y >> 6);
}

public int getRegionId() {
return ((getRegionX() << 8) + getRegionY());
}

public int getLocalX(WorldTile tile, int mapSize) {
return x - 8 * (tile.getChunkX() - (Settings.MAP_SIZES[mapSize] >> 4));
}

public int getLocalY(WorldTile tile, int mapSize) {
return y - 8 * (tile.getChunkY() - (Settings.MAP_SIZES[mapSize] >> 4));
}

public int getLocalX(WorldTile tile) {
return getLocalX(tile, 0);
}

public int getLocalY(WorldTile tile) {
return getLocalY(tile, 0);
}

public int getLocalX() {
return getLocalX(this);
}

public int getLocalY() {
return getLocalY(this);
}

public int get18BitsLocationHash() {
return getRegionY() + (getRegionX() << 8) + (plane << 16);
}

public int get30BitsLocationHash() {
return y + (x << 14) + (plane << 28);
}

public boolean withinDistance(WorldTile tile, int distance) {
if (tile.plane != plane)
return false;
int deltaX = tile.x - x, deltaY = tile.y - y;
return deltaX <= distance && deltaX >= -distance && deltaY <= distance
&& deltaY >= -distance;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + plane;
result = prime * result + x;
result = prime * result + y;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
WorldTile other = (WorldTile) obj;
if (plane != other.plane)
return false;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}

public boolean withinDistance(WorldTile tile) {
if (tile.plane != plane)
return false;
// int deltaX = tile.x - x, deltaY = tile.y - y;
return Math.abs(tile.x - x) <= 15 && Math.abs(tile.y - y) <= 15;// deltaX
// <= 14
// &&
// deltaX
// >=
// -15
// &&
// deltaY
// <= 14
// &&
// deltaY
// >=
// -15;
}

public int getCoordFaceX(int sizeX) {
return getCoordFaceX(sizeX, -1, -1);
}

public static final int getCoordFaceX(int x, int sizeX, int sizeY,
int rotation) {
return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
}

public static final int getCoordFaceY(int y, int sizeX, int sizeY,
int rotation) {
return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
}

public int getCoordFaceX(int sizeX, int sizeY, int rotation) {
return x + ((rotation == 1 || rotation == 3 ? sizeY : sizeX) - 1) / 2;
}

public int getCoordFaceY(int sizeY) {
return getCoordFaceY(-1, sizeY, -1);
}

public int getCoordFaceY(int sizeX, int sizeY, int rotation) {
return y + ((rotation == 1 || rotation == 3 ? sizeX : sizeY) - 1) / 2;
}

}

正如我之前提到的,我认为它没有任何问题。

最佳答案

只有当某个要序列化的对象没有实现java.io.Serialized接口(interface)时才会抛出NotSerializedException

我尝试模拟该问题并复制了您的类(class)。然后我创建了一个示例 WorldTile 类,如下所示:

public class WorldTile implements Serializable {

short x = 4;
byte y = 1;
}

我的代码如下,并且运行良好:

File file = new File("obj.txt");
file.createNewFile();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new InteractedObject(4, new WorldTile(), 5));
System.out.println("success");

您未能实现此目标的唯一原因可能是您的 WorldTile 类。

编辑:

另一个问题可能是您无法运行最新版本的文件。您是否保存了应用程序中的所有 .java 文件?

此外,您确定您已实现的 Serialized 接口(interface)是 indedd java.io.Serialized 吗?

关于java - NotSerializedException 实现 Serialized 的序列化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325586/

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