-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest3.java
More file actions
66 lines (45 loc) · 1.28 KB
/
Copy pathTest3.java
File metadata and controls
66 lines (45 loc) · 1.28 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
public class Test3 {
public static void main(String[] agrs) {
Calculator cal = new Calculator();
double r = 0.0;
try {
r = cal.divide(10, 0);
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
System.out.println("任何情况下都会走到这个分支!!!");
if(cal.isSwichOn()) {
cal.close();
}
}
}
}
class Calculator {
private boolean isSwichOn = false;
public boolean isSwichOn() {
return isSwichOn;
}
public void setSwichOn(boolean swichOn) {
this.isSwichOn = isSwichOn;
}
public void open() {
isSwichOn = true;
System.out.println("打开计算器电源,不过记得用完要关掉哦!!!");
}
public double divide(int a, int b) throws Exception {
double result = 0d;
open();
if (b!=0) {
result = a / b;
} else {
throw new Exception("严重警告:不能被0除!!!");
}
System.out.println(result);
close();
return result;
}
public void close() {
System.out.println("用完关掉电源,不然会爆!!!");
isSwichOn = false;
}
}