-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseInterface.java
More file actions
141 lines (120 loc) · 2.84 KB
/
BaseInterface.java
File metadata and controls
141 lines (120 loc) · 2.84 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
131
132
133
134
135
136
137
138
139
140
141
package class_ex;
import java.util.logging.Logger;
/**
* interface class 範例
*
* Java不支援多重繼承,但是interface是可以使用多重繼承概念。
*
* ! 兩物件即使介面完全一樣, 也可能會有完全不同的實作。
*
*/
public class BaseInterface {
public static void main(String[] args) {
BirdFly birdFly = new BirdFly();
birdFly.seagullFly();
birdFly.sparrowFly();
birdFly.eat();
}
}
/**
* ------- -------
* |Seagull| |Sparrow|
* ------- -------
* ^ ^
* | | implements
* \ /
* \ /
* -------
* |BirdFly|
* -------
*/
interface Seagull {
void seagullFly();
void eat();
}
interface Sparrow {
void sparrowFly();
void eat();
}
class BirdFly implements Seagull, Sparrow {
Logger logger = Logger.getLogger("BirdFly");
public void seagullFly() {
logger.info("seagull fly...");
}
public void sparrowFly() {
logger.info("sparrow fly...");
}
public void eat() {
// interface 命名方法相同一樣可以,不影響實作部分。
logger.info("BirdFly eat...");
}
}
/**
* ------- -------
* |Seagull| |Sparrow|
* ------- -------
* ^ ^
* | | extends
* \ /
* \ /
* ---------
* |AnimalFly|
* ---------
* ^
* | implements
* --------
* |FinalFly|
* --------
*/
interface AnimalFly extends Seagull, Sparrow {
void animalFly();
}
class FinalFly implements AnimalFly {
Logger logger = Logger.getLogger("FinalFly");
public void seagullFly() {
logger.info("seagull fly...");
}
public void sparrowFly() {
logger.info("sparrow fly...");
}
public void animalFly() {
logger.info("animal fly...");
}
public void eat() {
logger.info("FinalFly eat...");
}
}
/**
* interface 命名方法相同一樣可以,不影響實作部分。
*
* -------- --------
* |Seagull2| |Sparrow2|
* -------- --------
* ^ ^
* | | implements
* \ /
* \ /
* ----------
* |AnimalFly2|
* ----------
*/
interface Seagull2 {
void seagullFly();
void showInfo();
}
interface Sparrow2 {
void sparrowFly();
void showInfo();
}
class AnimalFly2 implements Seagull2, Sparrow2 {
Logger logger = Logger.getLogger("AnimalFly2");
public void seagullFly() {
logger.info("seagull fly...");
}
public void sparrowFly() {
logger.info("sparrow fly...");
}
public void showInfo() {
logger.info("call showInfo...");
}
}