forked from eaglesky/JavaPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDog.java
More file actions
54 lines (44 loc) · 1.69 KB
/
Dog.java
File metadata and controls
54 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
public class Dog extends Animal implements Serializable {
public String name = "";
public Dog(int age, int size, String name) {
super(age, size);
this.name = name;
}
public String toString() {
return super.toString() + ", Dog's name = " + name;
}
private void writeObject(ObjectOutputStream out) throws IOException {
// Take care of this class's field first by calling defaultWriteObject
out.defaultWriteObject();
/*
* Since the superclass does not implement the Serializable interface
* we explicitly do the saving... Since these fields are not private
* we can access them directly. If they were private, the superclass
* would have to implement get and set methods that would allow the
* subclass this necessary access for proper saving.
*/
out.writeInt(age);
out.writeInt(size);
out.writeObject(habitat);
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
/*
* Take care of this class's fields first by calling
* defaultReadObject
*/
in.defaultReadObject();
/*
* Since the superclass does not implement the Serializable
* interface we explicitly do the restoring... Since these fields
* are not private we can access them directly. If they were
* private, the superclass would have to implement get and set
* methods that would allow the subclass this necessary access
* for proper saving or restoring.
*/
age = in.readInt();
size = in.readInt();
habitat = (Habitat)in.readObject();
}
}