-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_9_1_RodentTest.java
More file actions
62 lines (51 loc) · 1019 Bytes
/
Copy path_9_1_RodentTest.java
File metadata and controls
62 lines (51 loc) · 1019 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
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
package unit9;
/**
* 使其成为一个抽象类。只要有可能,就将Rodent的方法声明为抽象方法·
* @author Administrator
*
*/
//class Rodent {
// void eat() {
// System.out.println("Rodent.eat()");
// }
//}
abstract class Rodent {
abstract void eat();//只是更改了基类为抽象类,方法声明为抽象方法,但是结果相同
}
class Mouse extends Rodent {
@Override
void eat() {
System.out.println("Mouse.eat()");
}
}
class Gerbil extends Rodent {
@Override
void eat() {
System.out.println("Gerbil.eat()");
}
}
class Hamster extends Rodent {
@Override
void eat() {
System.out.println("Hamster.eat()");
}
}
public class _9_1_RodentTest {
public static void live(Rodent r) {
r.eat();
}
static void liveAll(Rodent[] rs) {
for (Rodent rodent : rs) {
rodent.eat();
}
}
public static void main(String[] args) {
Rodent[] rodents = { new Mouse(), new Gerbil(), new Hamster() };
liveAll(rodents);
}
}
/*
Mouse.eat()
Gerbil.eat()
Hamster.eat()
*/