-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_9_7_RodentInterface.java
More file actions
59 lines (49 loc) · 1.02 KB
/
Copy path_9_7_RodentInterface.java
File metadata and controls
59 lines (49 loc) · 1.02 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
package unit9;
/**
* 修改第8章中的练习9,是Rodent成为一个接口
* @author Administrator
*
*/
interface Rodent9 {
void eat();
}
//implement某个interface,interface中的方法在implement后必须是public
class Mouse9 implements Rodent9 {
@Override
public void eat() {
System.out.println("Mouse.eat()");
}
}
class Gerbil9 implements Rodent9 {
@Override
public void eat() {
System.out.println("Gerbil.eat()");
}
}
class Hamster9 implements Rodent9 {
@Override
public void eat() {
System.out.println("Hamster.eat()");
}
}
public class _9_7_RodentInterface {
public static void live(Rodent9 r) {
r.eat();
}
static void liveAll(Rodent9[] rs) {
for (Rodent9 rodent : rs) {
rodent.eat();
}
}
public static void main(String[] args) {
Rodent9[] rodents = { new Mouse9(), new Gerbil9(), new Hamster9() };
liveAll(rodents);
}
}
/*
仅仅修改Rodent为接口,并删除Rodent中的方法实体;
同时将子类的extends改为implement即可;
Mouse.eat()
Gerbil.eat()
Hamster.eat()
*/