java編程幾個(gè)類,,可以實(shí)現(xiàn)動(dòng)物園中幾個(gè)科下動(dòng)物的增刪改查
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
importjava.util.ArrayList;
importjava.util.List;
?
classAnimal?{
????publicString?name;
????publicString?color;
?
????publicString?toString()?{
????????return?Name:?+?this.name?+???Color:?+?this.color;
????}
?
????/**
?????*?動(dòng)物吃東西
?????*?
?????*?@param?foodName
?????*????????????食物名稱
?????*/
????publicvoideat(String?foodName)?{
?
????}
}
?
classDog?extendsAnimal?{
?
????publicDog()?{
?
????}
?
????publicDog(String?name,?String?color)?{
????????this.name?=?name;
????????this.color?=?color;
????}
?
????publicvoideat(String?foodName)?{
????????System.out.println(Dog:?+?this.name?+??like?to?eat:?+?foodName);
????}
?
}
?
classCat?extendsAnimal?{
?
????publicCat()?{
?
????}
?
????publicCat(String?name,?String?color)?{
????????this.name?=?name;
????????this.color?=?color;
????}
?
????publicvoideat(String?foodName)?{
????????System.out.println(Cat:?+?this.name?+??like?to?eat:?+?foodName);
????}
?
}
?
classTiger?extendsAnimal?{
?
????publicTiger()?{
?
????}
?
????publicTiger(String?name,?String?color)?{
????????this.name?=?name;
????????this.color?=?color;
????}
?
????publicvoideat(String?foodName)?{
????????System.out.println(Tiger:?+?this.name?+??like?to?eat:?+?foodName);
????}
?
}
?
classLion?extendsAnimal?{
?
????publicLion()?{
?
????}
?
????publicLion(String?name,?String?color)?{
????????this.name?=?name;
????????this.color?=?color;
????}
?
????publicvoideat(String?foodName)?{
????????System.out.println(Tiger:?+?this.name?+??like?to?eat:?+?foodName);
????}
?
}
?
publicclassZoo?{
?
????publicstaticList<Animal>?zooList?=?newArrayList<Animal>();
?
????/**
?????*?添加Animal對(duì)象
?????*?
?????*?@param?obj
?????*/
????publicvoidaddAnimal(Animal?obj)?{
????????zooList.add(obj);
????}
?
????/**
?????*?根據(jù)animal的名字刪除對(duì)象
?????*?
?????*?@param?obj
?????*????????????Animal對(duì)象
?????*/
????publicvoiddeleteAnimal(Animal?obj)?{
????????booleandeleteFlag?=?false;
????????for(intindex?=?0;?null!=?zooList?&&?index?<?zooList.size();?index++)?{
????????????if(zooList.get(index).name.equals(obj.name))?{
????????????????zooList.remove(index);
????????????????deleteFlag?=?true;
????????????????System.out.println(刪除:?+?obj?+??成功);
????????????}
?
????????}
?
????????if(!deleteFlag)?{
????????????System.out.println(找不到該動(dòng)物:?+?obj);
????????}
?
????}
?
????/**
?????*?更新Animal對(duì)象信息
?????*?
?????*?@param?obj
?????*????????????Animal對(duì)象
?????*/
????publicvoidupdateAnimal(Animal?obj)?{
????????booleanflag?=?false;
????????
????????for(intindex?=?0;?null!=?zooList?&&?index?<?zooList.size();?index++)?{
????????????if(zooList.get(index).name.equals(obj.name))?{
????????????????zooList.get(index).name?=?obj.name;
????????????????zooList.get(index).color?=?obj.color;
????????????????flag?=?true;
????????????????System.out.println(\n修改成功);
????????????}
?
????????}
????????
????????if(!flag)?{
????????????System.out.println(找不到該動(dòng)物:?+?obj);
????????}
????}
?
????/**
?????*?根據(jù)動(dòng)物名字查詢
?????*?
?????*?@param?animalName
?????*????????????動(dòng)物名字
?????*/
????publicvoidcheckAniaml(String?animalName)?{
????????booleanflag?=?false;
????????for(intindex?=?0;?null!=?zooList?&&?index?<?zooList.size();?index++)?{
????????????if(zooList.get(index).name.equals(animalName))?{
????????????????System.out.println(zooList.get(index));
????????????????flag?=true;
????????????}
?
????????}
????????if(!flag)?{
????????????System.out.println(找不到該動(dòng)物:?+?animalName);
????????}
????}
?
????publicvoiddisplay(List<Animal>?list)?{
????????for(intindex?=?0;?null!=?list?&&?index?<?list.size();?index++)?{
????????????System.out.println(\n+?list.get(index));
????????}
????}
?
????publicstaticvoidmain(String[]?args)?{
?
????????Zoo?zoo?=?newZoo();
?
????????zoo.addAnimal(newDog(dog1,?black));
????????zoo.addAnimal(newDog(dog2,?black));
?
????????zoo.addAnimal(newLion(Lion1,?black));
????????zoo.addAnimal(newLion(Lion2,?black));
?
????????zoo.addAnimal(newCat(cat1,?yellow));
????????zoo.addAnimal(newCat(cat2,?yellow));
?
????????zoo.addAnimal(newTiger(Tiger1,?yellow));
????????zoo.addAnimal(newTiger(Tiger2,?yellow));
????????
????????
????????zoo.updateAnimal(newDog(dog1,somethingselse));
????????zoo.display(zooList);
????????
????????zoo.deleteAnimal(newDog(dog2,));
????????zoo.display(zooList);
????????
????????zoo.checkAniaml(Lion1);
?
????}
}
本網(wǎng)站文章僅供交流學(xué)習(xí) ,不作為商用,, 版權(quán)歸屬原作者,部分文章推送時(shí)未能及時(shí)與原作者取得聯(lián)系,若來源標(biāo)注錯(cuò)誤或侵犯到您的權(quán)益煩請(qǐng)告知,,我們將立即刪除.