使用ObjectInputStream

时间:2015-05-05 23:47:35

标签: java

我试图为我正在开发的游戏实施保存/加载功能。基本上,我有一个主类,它执行所有渲染并更新游戏状态对象(游戏状态对象保持玩家,敌人及其各自的位置,速度等)。所以我想我只是将游戏状态对象保存到文件usingObjectOutputStream并加载ObjectInputStream。保存/加载似乎工作正常,直到我在播放器类的String字段上执行if语句。我重新编写了尽可能短的代码来重现奇怪的行为。

玩家类:

import java.io.Serializable;

public class Player implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = -5933597609049497854L;
    private String type;

    public Player()
    {
        type = "evil";
    }

    public String getType()
    {
        return type;
    }


    public void setType(String type)
    {
        this.type = type;
    }
}

主要课程:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main
{
    private Player player;

    public Main()
    {
        player = new Player();

        System.out.println("player.getType(): ");
        System.out.println(player.getType());
        System.out.println("player.getType() == evil: ");
        System.out.println(player.getType() == "evil");

        saveGame();

        loadGame();

        System.out.println("player.getType(): ");
        System.out.println(player.getType());
        System.out.println("player.getType() == evil: ");
        System.out.println(player.getType() == "evil");
    }

    public static void main(String[] Args)
    {
        new Main();
    }

    private void saveGame()
    {
        try
        {
            FileOutputStream out = new FileOutputStream("save");

            ObjectOutputStream objectOut = new ObjectOutputStream(out);

            objectOut.writeObject(player);

            objectOut.close();

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void loadGame()
    {
        try
        {

            FileInputStream fin = new FileInputStream("save");

            ObjectInputStream objectIn = new ObjectInputStream(fin);

            player = null;

            player = (Player) objectIn.readObject();

            objectIn.close();

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

我从程序中收到以下输出:

player.getType():
邪恶
player.getType()== evil:
真正
player.getType():
邪恶
player.getType()== evil:

为什么字段type未被识别为" evil"?

1 个答案:

答案 0 :(得分:0)

因为使用- (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { [self highlight]; } else { [self unhighlight]; } } - (void)highlight { UIColor *currentLayerColor = [[UIColor alloc] initWithCGColor:self.layer.borderColor]; UIColor *newLayerColor = [UIColor blueColor]; CABasicAnimation *color = [CABasicAnimation animationWithKeyPath:@"borderColor"]; color.fromValue = (__bridge id)currentLayerColor.CGColor; color.toValue = (__bridge id)newLayerColor.CGColor; self.layer.borderColor = newLayerColor.CGColor; color.duration = 2.0; [self.layer addAnimation:color forKey:nil]; } - (void)unhighlight { UIColor *currentLayerColor = [[UIColor alloc] initWithCGColor:self.layer.borderColor]; UIColor *newLayerColor = [UIColor redColor]; CABasicAnimation *color = [CABasicAnimation animationWithKeyPath:@"borderColor"]; color.fromValue = (__bridge id)currentLayerColor.CGColor; color.toValue = (__bridge id)newLayerColor.CGColor; self.layer.borderColor = newLayerColor.CGColor; color.duration = 2.0; [self.layer addAnimation:color forKey:nil]; } 测试Object相等性。如果使用.equals,则表示您正在测试引用相等性,并且两个等效值实例可能(以及序列化将具有不同的引用)。你需要像

这样的东西
==