CodeGym /Kursus Jawa /Inti Jawa /Nyimpen array Obyek

Nyimpen array Obyek

Inti Jawa
tingkat , wulangan
kasedhiya

"Halo, Amigo! Dina iki kita bakal sinau babagan topik liyane sing menarik. Khusus, nyimpen lan ngemot (rekonstruksi) obyek . Upamane kita duwe kelas Kucing:"

Kode
class Cat
{
 public String name;
 public int age;
 public int weight;
}

Lan ayo ngomong kita pengin nambah mekanisme trep kanggo nyimpen lan loading saka file.

Kita bisa nindakake kaya iki:

Kode
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());
    }
}

"Wah! Gampang banget! Kita mung nulis nilai saben argumen, siji ing saben baris. Nalika mbukak file, kita maca kanthi urutan sing padha. Iku solusi sing sampurna."

"Matur nuwun, Amigo. Saiki sampeyan bisa nulis cara nyimpen lan mbukak kanggo klompok kelas iki:"

Kode
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;
}

Sampeyan duwe obyek Manungsa, sing bisa duwe siji asu lan siji kucing.

"Aku duwe solusi:"

Kode
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());
    }
}
Kode
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());
    }
}
Kode
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);
    }
}

"Iku solusi sing apik banget. Nanging apa sing kedadeyan yen manungsa duwe asu nanging ora ana kucing?"

Endi sing null mriksa?

"Aku bakal ndandani saiki:"

Kode
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);
    }
}

"Iki isih ora bener. Sampeyan duwe loro kesalahan:"

1) Wong bisa uga ora duwe kucing utawa asu, nanging isih bakal digawe nalika cara mbukak diarani

2) Yen mung nyimpen asu, data kasebut bakal diwaca kucing nalika dimuat.

"Lha, aku kudu piye?"

"Kita ora bisa skip nulis variabel, yen ora, kita bakal duwe masalah nalika maca . Sampeyan kudu nggawe manawa variabel sing null sak operasi nyimpen disetel kanggo null sak operasi mbukak. Punika versi sandi: "

Kode
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);
        }
    }
}

"Ya, aku seneng karo solusi iki."

"Ya, iku apik."

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