-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceClassExample.java
More file actions
184 lines (151 loc) · 3.97 KB
/
InterfaceClassExample.java
File metadata and controls
184 lines (151 loc) · 3.97 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
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
package class_ex;
/**
* 繼承 interface 範例
*
*/
public class InterfaceClassExample {
public static void main(String[] args) {
// Airplane, Bird example.
Fly bird = new Bird("angry bird"); // upcasting
bird.flying();
bird.getName();
Fly airplane = new Airplane("Boeing 747 airplane"); // upcasting
airplane.flying();
airplane.getName();
// Car example. (主要理解default, static, private method in the interface class.)
Car<String> car = new Car<>("Toyota");
System.out.println("[Step 1] " + car.alarmOn());
System.out.println("[Step 2] " + car.getBrand());
System.out.println("[Step 3] " + car.run());
System.out.println("------------------------------------");
Car<String> result = car.execute();
System.out.println("execute time: " + result.getExecuteTime());
}
}
/**
* 使用單一 interface 介面來實作
* - interface: Fly
* - class: Bird, Airplane
*/
interface Fly {
void getName();
void flying();
}
class Bird implements Fly {
private String name;
Bird(String name) {
this.name = name;
}
@Override
public void getName() {
System.out.println("I am a " + name + ".");
}
@Override
public void flying() {
System.out.println("Bird is flying...");
}
}
class Airplane implements Fly {
private String name;
Airplane(String name) {
this.name = name;
}
public void getName() {
System.out.println("I am a " + name + ".");
}
public void flying() {
System.out.println("Airplane is flying...");
}
}
/**
* Interface class 規則與範例
* - 介面內 private method 存在時,可提供介面內程式碼重複使用。
* - 實作的類別將無法呼叫介面內 private method。
* - default method 則可以被實作類別呼叫。
*
* default 擴充interface新功能,如果不使用 default,interface不允許方法內有實作內容。
*/
interface Vehicle<T> {
String getBrand();
String run();
default String alarmOn() {
method1();
return "turn on alarm.";
}
default String alarmOff() {
return "turn off alarm.";
}
static int speedUp(int speed) {
return speed + 50;
}
/**
* 此 private method 只能被允許在介面內使用
*/
private void method1() {
method3();
System.out.println("This is a method 1.");
}
private void method2() {
System.out.println("This is a method 2.");
}
private void method3() {
System.out.println("This is a method 3.");
method2();
}
/**
* TODO: 是否可以使用通用介面來讓class繼承並實作細節
*/
public Vehicle<?> execute();
}
/**
* 實作 interface: Vehicle + Generic
*/
class Car<T> implements Vehicle<T> {
private String brand;
private int execute_time;
Car(String brand) {
this.brand = brand;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String run() {
return "driving car safely.";
}
public Car<T> step4() {
System.out.println("call step 4.");
execute_time++;
return this;
}
public Car<T> step5() {
System.out.println("call step 5.");
execute_time++;
return this;
}
public Car<T> step6() {
System.out.println("call step 6.");
execute_time++;
return this;
}
public Car<T> reverseStep() {
System.out.println(" call reverse step.");
execute_time--;
return this;
}
@Override
public Car<T> execute() {
execute_time = 0;
this.step4()
.reverseStep()
.step5()
.step6()
.reverseStep();
// System.out.println("execute_time: " + execute_time);
return this;
}
public int getExecuteTime() {
return this.execute_time;
}
}