class="origin_image zh-lightbox-thumb lazy" width="982" alt="" src="/Upload/Images/2020062816/B7C83044B6A4D6D5.jpg">
monospace; font-size: inherit; background-color: inherit;" class="language-text">class Outer {
Nested nested;
Nested getNested() {
return nested;
}
public Outer(){
// nested = new Nested();
}
}
class Nested {
Inner inner;
Inner getInner() {
return inner;
}
public Nested() {
// inner = new Inner();
}
}
class Inner {
String foo = "Jerry";
String getFoo() {
return foo;
}
}
?
为了打印嵌套层数很深的foo:
public void test1(){
Outer outer = new Outer();
if (outer != null && outer.nested != null && outer.nested.inner != null) {
System.out.println(outer.nested.inner.foo);
}
}
?
# 文艺青年的写法
public void test2(){
Optional.of(new Outer()).map(Outer::getNested).map(Nested::getInner).map(Inner::getFoo)
.ifPresent(System.out::println);
}
More discussion of ABAP, Java and JavaScript could be found from my Wechat article Jerry的ABAP, Java和JavaScript乱炖:?Jerry的ABAP, Java和JavaScript乱炖