Почему запись объекта в файл делает ошибку здесь?(Используя ObjectOutputStream) - PullRequest
0 голосов
/ 13 сентября 2018

Я довольно новичок в Java. В настоящее время я делаю игру и пытаюсь создать метод для сохранения и загрузки созданных мной объектов. Вот как я это сделал.

У меня есть класс Боба, он расширяет Character и реализует Serializable. Объект, который я хотел бы записать в файл: "Боб"

Я пробовал с пустым объектом, и он работает. Но с этим объектом "Боб", он как-то делает ошибку .. Понятия не имею почему. В чем проблема?

     public class Main implements Serializable {  
       static Character Bob= new Bob();
       static Character James= new James();


     public static void main(String[] args) {   
            try {
                    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Members.txt"));


                out.writeObject(bob);


            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }
















  import java.io.Serializable;
import java.util.Random;
import java.util.Scanner;

public class Character implements Serializable {
    private int hp;
    private int mp;
    private int att;
    private int maxHp;
    private int maxMp;
    private String skill;
    boolean blnBlock = false;
    Scanner sc = new Scanner(System.in);
    private int exp;
    private int lv;
    private int lvExp;
    static int money = 10000;
    static int hpNum = 1;
    static int mpNum = 1;
    private int moneyMonster;
    private Portion porHHContain;
    private Portion porMMContain;

    public int getMaxHp() {
        return maxHp;
    }

    public void setMaxHp(int maxHp) {
        this.maxHp = maxHp;
    }

    public int getMaxMp() {
        return maxMp;
    }

    public void setMaxMp(int maxMp) {
        this.maxMp = maxMp;
    }

    public Portion getPorHHContain() {
        return porHHContain;
    }

    public void setPorHHContain(Portion porHHContain) {
        this.porHHContain = porHHContain;
    }

    public Portion getPorMMContain() {
        return porMMContain;
    }

    public void setPorMMContain(Portion porMMContain) {
        this.porMMContain = porMMContain;
    }

    public int getMoneyMonster() {
        return moneyMonster;
    }

    public void setMoneyMonster(int moneyMonster) {
        this.moneyMonster = moneyMonster;
    }

    public int getLvExp() {
        return lvExp;
    }

    public void setLvExp(int lvExp) {
        this.lvExp = lvExp;
    }

    public int getLv() {
        return lv;
    }

    public void setLv(int lv) {
        this.lv = lv;
    }

    public int getExp() {
        return exp;
    }

    public void setExp(int exp) {
        this.exp = exp;
    }

    public boolean isBlnBlock() {
        return blnBlock;
    }

    public void setBlnBlock(boolean blnBlock) {
        this.blnBlock = blnBlock;
    }

    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 getAtt() {
        return att;
    }

    public void setAtt(int att) {
        this.att = att;
    }

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }

    public String toString(String str) {
        return str;

    }

    public void attack(Character enemy) {

        Random rand2 = new Random();
        int attChance = rand2.nextInt(10) + 1;
        if (attChance < 8) {
            if(Main.blnLa) {
                Main.boss1Count++;
            }if(Main.blnVegas) {
                Main.boss2Count++;
            }
            if (enemy.getHp() <= this.getAtt()) {
                enemy.setHp(0);
                System.out.println("** " + this + " attacked " + enemy + " by " + this.getAtt() + "!!");
                sc.nextLine();
                System.out.println("** " + enemy + " knocked down!");
                sc.nextLine();
                this.setExp(this.getExp() + enemy.getExp());
                System.out.println("** " + this + ", has earned " + enemy.getExp() + "exp from " + enemy + "!  Exp:"
                        + this.getExp() + "/" + this.getLvExp());
                sc.nextLine();
                Main.exp(this);
                this.setLv(this.getLvExp() + this.getLvExp() / 2);
                Character.money = Character.money + enemy.getMoneyMonster();
                System.out.println("** $" + enemy.getMoneyMonster() + " acquired from " + enemy + "  $"
                        + enemy.getMoneyMonster() + "/" + Character.money);
                Random rand = new Random();
                Random rand1 = new Random();
                int portChance = rand.nextInt(10) + 1;
                if (portChance >= 8) {
                    int twoChance = rand1.nextInt(2) + 1;
                    if (twoChance == 1) {
                        System.out.println("** You acquired 1 of " + enemy.porHHContain + ".");
                        Character.hpNum++;
                    } else {
                        System.out.println("** You acquired 1 of " + enemy.porMMContain + ".");
                        Character.mpNum++;
                    }
                    sc.nextLine();
                }
                if (Main.boss1.getHp() == 0) {
                    Main.blnLaBossKilled = true;
                }
                if (Main.boss2.getHp() ==0) {
                    Main.blnVegasBossKilled=true;
                }
                Main.deadEnemyCount++;
                if (Main.deadEnemyCount == 3) {
                    Main.blnDungeon = true;
                }

            } else {
                enemy.setHp(enemy.getHp() - this.getAtt());
                System.out.println("** " + this + " attacked " + enemy + " by " + this.getAtt() + "!!");

            }
            sc.nextLine();
        } else {
            System.out.println("!!" + this + " missed the target!");
        }
    }

    public void useSkill(Character enemy) {
        Random rand = new Random();
        int crit = rand.nextInt(10) + 1;
        this.setMp(this.getMp() - 15);
        System.out.println("** Used 15 MP");
        if (crit < 9) {
            int att = (int) (this.getAtt() * 1.5);
            System.out.println("** " + this + "'s " + this.getSkill() + "!!");
            sc.nextLine();
            skillAtt(enemy, att);
        } else {
            int att = (int) (this.getAtt() * 2);
            System.out.println("** " + this + "'s " + this.getSkill() + "!!");
            System.out.println("** Critical!!");
            sc.nextLine();
            skillAtt(enemy, att);

        }

    }

    private void skillAtt(Character enemy, int att) {
        if (enemy.getHp() <= att) {
            enemy.setHp(0);
            System.out.println("** " + enemy + " got damaged by " + att + "!!");
            sc.nextLine();
            System.out.println("** " + enemy + " knocked down!");
            sc.nextLine();
            Main.deadEnemyCount++;
            if (Main.deadEnemyCount == 3) {
                Main.blnDungeon = true;
            }
        } else {
            enemy.setHp(enemy.getHp() - att);
            System.out.println("** " + enemy + " got damaged by " + att + "!!");
            sc.nextLine();
        }
    }

}

class Bob extends Character  implements Serializable {

    Bob() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("A");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);

    }

    public String toString() {
        return "Bob";

    }

}

class James extends Character   implements Serializable{

    James() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("B");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "James";

    }

}

class Jacob extends Character   implements Serializable{

    Jacob() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("C");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Jacob";

    }

}

class Alex extends Character   implements Serializable{

    Alex() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("D");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Alex";

    }

}

class Peter extends Character   implements Serializable{

    Peter() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("E");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Peter";

    }

}

class Bryan extends Character  implements Serializable {

    Bryan() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("E");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Bryan";

    }

}

class Phil extends Character   implements Serializable{

    Phil() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("F");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Phil";

    }

}

class Ryan extends Character   implements Serializable{

    Ryan() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("G");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);

    }

    public String toString() {
        return "Ryan";

    }

}

class Cris extends Character  implements Serializable {

    Cris() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("S");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Cris";

    }

}

class Sean extends Character  implements Serializable {

    Sean() {
        this.setMaxHp(100);
        this.setMaxMp(30);
        this.setHp(100);
        this.setMp(30);
        this.setAtt(20);
        this.setSkill("T");
        this.setExp(0);
        this.setLv(1);
        this.setLvExp(800);
    }

    public String toString() {
        return "Sean";

    }

}

class Enemy1 extends Character  implements Serializable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    Enemy1(int hp, int mp, int att, int exp, int moneyMonster, String name) {
        this.setAtt(att);
        this.setHp(hp);
        this.setMp(mp);
        this.setExp(exp);
        this.name = name;
        this.setMoneyMonster(moneyMonster);
        Portion Hpo = new Portion("HP portion");
        Portion Mpo = new Portion("MP portion");

        this.setPorHHContain(Hpo);
        this.setPorMMContain(Mpo);

    }

    public String toString() {
        return this.name;

    }

    public void attack(Character chosenMember) {
        Random rand = new Random();
        int blockChance = rand.nextInt(10)+1;
        if (chosenMember.isBlnBlock() == true) {
            if(blockChance <3) {
                System.out.println("!!!Block failed!!");
                attackY(chosenMember);
            }else {
            System.out.println(chosenMember + " blocked the attack!!");
            sc.nextLine();
            chosenMember.setBlnBlock(false);
            }
        } else {
            if (this.name.equals("Drunken A")) {
                special1(chosenMember);
            } else {

                attackY(chosenMember);

            }

        }
        sc.nextLine();
    }

    private void special1(Character chosenMember) {
        Random rand = new Random();
        int chance = rand.nextInt(10) + 1;
        if (chance < 8) {
            int att = (int) this.getAtt() * 3;
            if (chosenMember.getHp() <= att) {
                chosenMember.setHp(0);
                System.out.println("** " + this + " just took another drug and became more powerful!!");
                sc.nextLine();
                System.out.println("** " + this + " attacked " + chosenMember + " by " + att + "!!");
                sc.nextLine();
                System.out.println("!!! " + chosenMember + " knocked down!");

                Main.deadTeamCount++;
                if (Main.deadTeamCount == 5) {
                    Main.blnDungeon = true;

                }

            } else {
                chosenMember.setHp(chosenMember.getHp() - att);
                System.out.println("** " + this + " just took another drug and became more powerful!!");
                System.out.println("** " + this + " attacked " + chosenMember + " by " + att + "!!");


            }
        } else {
            attackY(chosenMember);
        }
    }

    private void attackY(Character chosenMember) {

        if(Main.blnLa&& Main.boss1Count==10) {
            System.out.println("**LA Mayor got super mad!!!!");
            sc.nextLine();
            int attBoss = this.getAtt()*2;
            if (chosenMember.getHp() <= attBoss) {
                chosenMember.setHp(0);
                System.out.println("** " + this + " attacked " + chosenMember + " by " + attBoss + "!!");
                sc.nextLine();
                System.out.println("!!! " + chosenMember + " knocked down!");
                Main.deadTeamCount++;
                if (Main.deadTeamCount == 5) {
                    Main.blnDungeon = true;

                }
                Main.boss1Count = 0;
            } else {
                chosenMember.setHp(chosenMember.getHp() - attBoss);
                System.out.println("** " + this + " attacked " + chosenMember + " by " + attBoss + "!!");


            }
        }else if(Main.blnVegas&&Main.boss2Count==7){
            System.out.println("**asdfasdf got super mad!!!!");
            sc.nextLine();
            int attBoss = this.getAtt()*2;
            if (chosenMember.getHp() <= attBoss) {
                chosenMember.setHp(0);
                System.out.println("** " + this + " attacked " + chosenMember + " by " + attBoss + "!!");
                sc.nextLine();
                System.out.println("!!! " + chosenMember + " knocked down!");
                Main.deadTeamCount++;
                if (Main.deadTeamCount == 5) {
                    Main.blnDungeon = true;

                }
                Main.boss2Count = 0;
            } else {
                chosenMember.setHp(chosenMember.getHp() - attBoss);
                System.out.println("** " + this + " attacked " + chosenMember + " by " + attBoss + "!!");


            }



        }else  if(chosenMember.getHp() <= this.getAtt()) {
            chosenMember.setHp(0);
            System.out.println("** " + this + " attacked " + chosenMember + " by " + this.getAtt() + "!!");
            sc.nextLine();
            System.out.println("!!! " + chosenMember + " knocked down!");

            Main.deadTeamCount++;
            if (Main.deadTeamCount == 5) {
                Main.blnDungeon = true;

            }

        } else {
            chosenMember.setHp(chosenMember.getHp() - this.getAtt());
            System.out.println("** " + this + " attacked " + chosenMember + " by " + this.getAtt() + "!!");


        }
    }

}

1 Ответ

0 голосов
/ 14 сентября 2018

Это может быть потому, что вы объявляете переменную bob типа Character вместо Bob. Можете ли вы попробовать объявить его типа Боба? Или сделайте сериализуемый ваш суперкласс (в этом случае вам не нужно делать сериализуемым Боба)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...