-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeTierExample.java
More file actions
82 lines (69 loc) · 1.86 KB
/
ThreeTierExample.java
File metadata and controls
82 lines (69 loc) · 1.86 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
package class_ex;
/**
* 三層架構:interface class, abstract class, concrete class.
*
* ! interface/abstract class 皆不能實體化
*
*/
public class ThreeTierExample {
public static void main(String[] args) {
ConcreteConvert cc = new ConcreteConvert("george", 178, 75);
double res = cc.calBMI();
System.out.println(res);
ConverterA ca = new ConverterA("mary", 166, 49);
double res2 = ca.calBMI();
System.out.println(res2);
}
}
interface Orgin {
/**
* 1. interface class 不能實體化
* !2. 介面變數必須要有初始值
* 3. interface class 可多重繼承 interface class.
*/
// String sex = "";
}
interface InterfaceConvert extends Orgin {
public double calBMI();
}
abstract class AbstractConvert implements InterfaceConvert {
/**
* 1. abstract class 算是部分可以定義或實作的class。
* !2. abstract class 也不能實體化
* 3. abstract class 也不可以多重繼承
*/
public String name;
public double height;
public double weight;
AbstractConvert(String name, double height, double weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
public String getName() {
return this.name;
}
public double getHeight() {
return this.height;
}
public double getWeight() {
return this.weight;
}
public double calBMI() {
return weight / Math.pow(height * 0.01, 2);
}
}
class ConcreteConvert extends AbstractConvert {
ConcreteConvert(String name, double height, double weight) {
super(name, height, weight);
}
}
class ConverterA extends AbstractConvert {
ConverterA(String n, double h, double w) {
super(n, h, w);
}
@Override
public double calBMI() {
return weight + height;
}
}