forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2.java
More file actions
80 lines (71 loc) · 2.18 KB
/
Copy pathExample2.java
File metadata and controls
80 lines (71 loc) · 2.18 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
package chapter_12;
/**
* 从对象中抽取值
*
* @author biezhi
* @date 2018/8/25
*/
public class Example2 {
static class Address {
String countryName;
String provinceName;
String cityName;
String areaName;
public Address(String countryName, String provinceName, String cityName, String areaName) {
this.countryName = countryName;
this.provinceName = provinceName;
this.cityName = cityName;
this.areaName = areaName;
}
}
// 上海 - 浦东
// public String part1(Address address) {
// String place = address.cityName;
// if (place == null) {
// place = address.provinceName;
// }
// if (place == null && "中国".equals(address.countryName)) {
// place = address.countryName;
// }
// if (place == null) {
// place = "未知城市";
// }
// if (address.areaName != null) {
// place += " - " + address.areaName;
// } else {
// place += " - 无人区";
// }
// return place;
// }
// 上海 - 浦东
public String part2(Address address) {
String provinceName = address.provinceName;
String cityName = address.cityName;
String areaName = address.areaName;
String firstHalf = ifNullThen("未知城市", cityName, provinceName);
String secondHalf = ifNullThen("无人区", areaName);
// ||
return firstHalf + " - " + secondHalf;
}
private String ifNullThen(String defaultValue, String...args){
if(null == args || args.length == 0){
return defaultValue;
}
for (String arg : args) {
if(arg != null){
return arg;
}
}
return defaultValue;
}
public static void main(String[] args) {
Example2 example2 = new Example2();
String place = example2.part2(
new Address("中国", null, "上海", "徐汇区")
);
place = example2.part2(
new Address("中国", "山西省", null, "迎泽区")
);
System.out.println(place);
}
}