Quantcast
Channel: Programação - GUJ
Viewing all articles
Browse latest Browse all 33093

Ajuda com serialização (Resolvido)

$
0
0

@WilliamLycan escreveu:

Olá, sou novo no fórum, gostaria que vocês me ajudassem com a serialização de um objeto. Tenho uma classe Char que tem atributos de um jogador de RPG, porém quando modifico o nome e carrego o objeto salvo com outro "nome" ele não carrega o atributo "nome" do objeto des-serializado, mas o que eu setei entre o salvamento e o carregamento.

public class Main {
    public static void main(String[] args) {
	    Char player = new Char("Will", 1, 100, 100, 40, 40);
		
	        player.save();
		
		player.setName("Bob");
		
		player.load(player);
		
		System.out.println(player.getName());
	    }
}

E a saída sempre dá "Bob" ao invés de Will. O que estou fazendo de errado?

E aqui está a classe Char:

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

public class Char implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private int level;
	private int hp;
	private int mp;
	private int atk;
	private int def;

public static long getSerialVersionID(){
	return serialVersionUID;
}

public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getLevel() {
	return level;
}
public void setLevel(int level) {
	this.level = level;
}
public int getHp() {
	return hp;
}
public void setHp(int hp) {
	this.hp = hp;
}
public int getMp() {
	return mp;
}
public void setMp(int mp) {
	this.mp = mp;
}
public int getAtk() {
	return atk;
}
public void setAtk(int atk) {
	this.atk = atk;
}
public int getDef() {
	return def;
}
public void setDef(int def) {
	this.def = def;
}

public void save() {
	try {
		FileOutputStream fileOut = new FileOutputStream("C:\\JavaIO\\player.save");
		ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
		objOut.writeObject(this);
		objOut.close();
		fileOut.close();
	} catch(IOException e) {
		System.out.println("Ocorreu um erro: " + e.getMessage());
	}
}

public void load(Char c) {
	try {
		FileInputStream fileIn = new FileInputStream("C:\\JavaIO\\player.save");
		ObjectInputStream objIn = new ObjectInputStream(fileIn);
		c = (Char)objIn.readObject();
		objIn.close();
		fileIn.close();
	} catch(IOException e) {
		System.out.println("Ocorreu um erro: " + e.getMessage());
	} catch(ClassNotFoundException e) {
		System.out.println("Ocorreu um erro: " + e.getMessage());
	}
}

public Char(String name, int level, int hp, int mp, int atk, int def) {
	this.name = name;
	this.level = level;
	this.hp = hp;
	this.mp = mp;
	this.atk = atk;
	this.def = def;
}

}

E mais uma coisa: devo criar uma classe separada para os métodos save() e load() e assim poder salvar qualquer coisa, o estado do jogador, o inventário etc..?

Mensagens: 11

Participantes: 3

Ler tópico completo


Viewing all articles
Browse latest Browse all 33093