forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrog.java
More file actions
130 lines (124 loc) · 3.47 KB
/
Copy pathFrog.java
File metadata and controls
130 lines (124 loc) · 3.47 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// polymorphism/Frog.java
// (c)2020 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Cleanup and inheritance
// {java polymorphism.Frog}
class Characteristic {
private String s;
Characteristic(String s) {
this.s = s;
System.err.println("Creating Characteristic " + s);
}
protected void dispose() {
System.err.println("disposing Characteristic " + s);
}
}
class Description {
private String s;
Description(String s) {
this.s = s;
System.err.println("Creating Description " + s);
}
protected void dispose() {
System.err.println("disposing Description " + s);
}
}
class LivingCreature {
private Characteristic p =
new Characteristic("is alive");
private Description t =
new Description("Basic Living Creature");
LivingCreature() {
System.err.println("LivingCreature()");
}
protected void dispose() {
System.err.println("LivingCreature dispose");
t.dispose();
p.dispose();
}
}
class Animal extends LivingCreature {
private Characteristic p =
new Characteristic("has heart");
private Description t =
new Description("Animal not Vegetable");
Animal() { System.err.println("Animal()"); }
@Override
protected void dispose() {
System.err.println("Animal dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
class Amphibian extends Animal {
private Characteristic p =
new Characteristic("can live in water");
private Description t =
new Description("Both water and land");
Amphibian() {
System.err.println("Amphibian()");
}
@Override
protected void dispose() {
System.err.println("Amphibian dispose");
t.dispose();
p.dispose();
super.dispose();
}
}
public class Frog extends Amphibian {
private Characteristic p =
new Characteristic("Croaks");
private Description t = new Description("Eats Bugs");
public Frog() { System.err.println("Frog()"); }
@Override
protected void dispose() {
System.err.println("Frog dispose");
t.dispose();
p.dispose();
super.dispose();
}
public static void main(String[] args) {
Frog frog = new Frog();
System.err.println("Bye!");
frog.dispose();
}
}
/* Output:
Creating Characteristic is alive
Creating Description Basic Living Creature
LivingCreature()
Creating Characteristic has heart
Creating Description Animal not Vegetable
Animal()
Creating Characteristic can live in water
Creating Description Both water and land
Amphibian()
Creating Characteristic Croaks
Creating Description Eats Bugs
Frog()
Bye!
Frog dispose
disposing Description Eats Bugs
disposing Characteristic Croaks
Amphibian dispose
disposing Description Both water and land
disposing Characteristic can live in water
Animal dispose
disposing Description Animal not Vegetable
disposing Characteristic has heart
LivingCreature dispose
disposing Description Basic Living Creature
disposing Characteristic is alive
*/
/**
* self-note:
*
* 对象销毁的顺序应该与初始化的顺序相反,以防一个对象依赖另一个对象。
* 属性销毁的顺序与声明的顺序相反(因为属性是按照声明顺序初始化的)。
* 首先进行派生类的清理工作,然后才是基类的清理。
* 这是因为派生类的清理可能调用基类的一些方法,所以基类组件这时得存活,不能过早地被销毁。
* 尽管通常不必进行清理工作,但万一需要时,就得谨慎小心地执行。
*/