CodeGym /Java Course /Java 核心 /保存對像數組

保存對像數組

Java 核心
等級 10 , 課堂 1
開放

“你好,阿米戈!今天我們將學習另一個有趣的話題。具體來說,保存和加載(重建)對象。假設我們有一個 Cat 類:”

代碼
class Cat
{
 public String name;
 public int age;
 public int weight;
}

假設我們想要添加一種方便的機制來保存和加載文件。

我們可以這樣做:

代碼
class Cat {
    public String name;
    public int age;
    public int weight;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.println(weight);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
        weight = Integer.parseInt(reader.readLine());
    }
}

“哇!這太簡單了!我們只需寫下每個參數的值,每行一個。當我們加載文件時,我們以相同的順序讀取它們。這是完美的解決方案。”

“謝謝,阿米戈。現在你可以為這組類編寫保存和加載方法了:”

代碼
class Cat
{
 public String name;
 public int age;
 public int weight;
}
class Dog
{
 public String name;
 public int age;
}
class Human
{
 public Cat cat;
 public Dog dog;
}

你有一個 Human 對象,它可以有一隻狗和一隻貓。

“我有辦法:”

代碼
class Cat {
    public String name;
    public int age;
    public int weight;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.println(weight);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
        weight = Integer.parseInt(reader.readLine());
    }
}
代碼
class Dog {
    public String name;
    public int age;

    public void save(PrintWriter writer) throws Exception {
        writer.println(name);
        writer.println(age);
        writer.flush();
    }

    public void load(BufferedReader reader) throws Exception {
        name = reader.readLine();
        age = Integer.parseInt(reader.readLine());
    }
}
代碼
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        cat.save(writer);
        dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {
        cat.load(reader);
        dog.load(reader);
    }
}

“這是一個很好的解決方案。但是如果一個人有狗但沒有貓會怎樣?”

空檢查在哪裡?

“我現在就修:”

代碼
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        if (cat != null)
            cat.save(writer);
        if (dog != null)
            dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {
        cat = new Cat();
        cat.load(reader);
        dog = new Dog();
        dog.load(reader);
    }
}

“還是不太對,你有兩個錯誤:”

1)一個人可能沒有貓或狗,但在調用 load 方法時仍然會創建它們

2)如果我們只保存一隻狗,它的數據在加載時會被貓讀取。

“嗯,我該怎麼辦?”

“我們不能跳過寫入變量,否則我們在讀取過程中會遇到問題。您需要確保在保存操作期間為 null 的變量在加載操作期間設置為 null。這是我的版本:”

代碼
public class Human {
    public Cat cat;
    public Dog dog;

    public void save(PrintWriter writer) throws Exception {
        String isCatPresent = cat != null ? "yes" : "no";
        writer.println(isCatPresent);
        writer.flush();

        if (cat != null)
            cat.save(writer);

        String isDogPresent = dog != null ? "yes" : "no";
        writer.println(isDogPresent);
        writer.flush();

        if (dog != null)
            dog.save(writer);
    }

    public void load(BufferedReader reader) throws Exception {

        String isCatPresent = reader.readLine();
        if (isCatPresent.equals("yes")) {
            cat = new Cat();
            cat.load(reader);
        }

        String isDogPresent = reader.readLine();
        if (isDogPresent.equals("yes")) {
            dog = new Dog();
            dog.load(reader);
        }
    }
}

“是的,我喜歡這個解決方案。”

“是啊,挺好的。”

留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION