-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoo.java
More file actions
36 lines (27 loc) · 695 Bytes
/
Copy pathZoo.java
File metadata and controls
36 lines (27 loc) · 695 Bytes
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
package src.thinkinginjava.Polymorphism8;
import java.util.Random;
/**
* Created by Ryan on 2017/1/25.
*/
public class Zoo {
public static void main(String[] args) {
Rodent[] rodents = new Rodent[10];
for(int i = 0; i < rodents.length; i++) {
rodents[i] = allocateRodent();
eat(rodents[i]);
}
}
public static void eat(Rodent r) {
r.eat();
}
public static Rodent allocateRodent() {
Random random = new Random();
switch (random.nextInt(2)) {
case 0:
return new Mouse();
case 1:
return new Gerbil();
}
return null;
}
}