准备工作
有下面这样一个类结构
class R1 {
private String t1;
public R1(String t1) {
this.t1 = t1;
}
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
}
class R2 {
private R1 r1;
public R2(R1 r1) {
this.r1 = r1;
}
public R1 getR1() {
return r1;
}
public void setR1(R1 r1) {
this.r1 = r1;
}
}
class R3 {
private R2 r2;
public R3(R2 r2) {
this.r2 = r2;
}
public R2 getR2() {
return r2;
}
public void setR2(R2 r2) {
this.r2 = r2;
}
}
class R4 {
private R3 r3;
public R4(R3 r3) {
this.r3 = r3;
}
public R3 getR3() {
return r3;
}
public void setR3(R3 r3) {
this.r3 = r3;
}
}
从R4对象中取出R1对象中的t1的值,并打印。其中任意对象可能为null。你会怎么做?
套娃?
private static void nested(R4 r4) {
String result = null;
if (r4 != null) {
R3 r3 = r4.getR3();
if (r3 != null) {
R2 r2 = r3.getR2();
if (r2 != null) {
R1 r1 = r2.getR1();
if (r1 != null) {
result = r1.getT1();
}
}
}
}
System.out.println("nested->" + result);
}
禁止套娃,技术太菜,开除了吧
lambda表达式取值?
private static void lambda(R4 r4) {
String result = Optional.ofNullable(r4)
.map(e -> e.getR3())
.map(e -> e.getR2())
.map(e -> e.getR1())
.map(e -> e.getT1())
.orElse(null);
System.out.println("lambda->" + result);
}
技术不错,可是老项目还在用java6或7
封装单独的方法?
private static void methodReturn(R4 r4) {
class RetClazz {
public String returnVal (R4 r4) {
if (r4 == null) {
return null;
}
R3 r3 = r4.getR3();
if (r3 == null) {
return null;
}
R2 r2 = r3.getR2();
if (r2 == null) {
return null;
}
R1 r1 = r2.getR1();
if (r1 == null) {
return null;
}
return r1.getT1();
}
}
String result = new RetClazz().returnVal(r4);
System.out.println("methodReturn->" + result);
}
懂得封装,技术不错
do while (false) 是什么鬼?
private static void dowhile(R4 r4) {
String result = null;
do {
if (r4 == null) {
break;
}
R3 r3 = r4.getR3();
if (r3 == null) {
break;
}
R2 r2 = r3.getR2();
if (r2 == null) {
break;
}
R1 r1 = r2.getR1();
if (r1 == null) {
break;
}
result = r1.getT1();
} while (false);
System.out.println("dowhile->" + result);
}
小伙子思路清奇,竟然用循环结构的break关键字,java大神要review你的代码,看你出什么妖蛾子,质量太差就开除你;C大神,嗯,这才是好码农,支持一波
这时需求变了,从R4对象中取出R1对象中的t1的值,并打印,但是当R2为null时,执行printR2null方法且不打印t1的值。其中任意对象可能为null
private static void printR2null (String method) {
System.out.println(method + ": r2==null");
}
我还套娃
private static void nested2(R4 r4) {
String result = null;
if (r4 != null) {
R3 r3 = r4.getR3();
if (r3 != null) {
R2 r2 = r3.getR2();
if (r2 != null) {
R1 r1 = r2.getR1();
if (r1 != null) {
result = r1.getT1();
}
} else {
printR2null("nested");
return;
}
}
}
System.out.println("nested->" + result);
}
虽然if嵌套太深,但好歹只加一两行代码就实现了
看我lambda大法
private static void lambda2(R4 r4) {
boolean[] r2null = {false};
String result = Optional.ofNullable(r4)
.map(e -> e.getR3())
.map(e -> {
R2 r2 = e.getR2();
r2null[0] = true;
return r2;
})
.map(e -> e.getR1())
.map(e -> e.getT1())
.orElse(null);
if (r2null[0]) {
printR2null("lambda");
return;
}
System.out.println("lambda->" + result);
}
什么鬼东西?还定义一个数组对象?普通对象行吗?行。 定义一个boolean类型的变量不行吗?呃,这个真不行,在lambda方法体内引用外面的变量就相当于在内部类中引用外面的final变量,看IDE报了个语法错误,气得java程序员直抓狂,为什么java不支持地址传递
还是封装单独的方法好用
private static void methodReturn2(R4 r4) {
class RetClazz {
public Map<String, Object> returnVal (R4 r4) {
Map<String, Object> map = new HashMap<>();
if (r4 == null) {
map.put("result", null);
return map;
}
R3 r3 = r4.getR3();
if (r3 == null) {
map.put("result", null);
return map;
}
R2 r2 = r3.getR2();
if (r2 == null) {
map.put("nullObject", R2.class);
map.put("result", null);
return map;
}
R1 r1 = r2.getR1();
if (r1 == null) {
map.put("result", null);
return map;
}
map.put("result", r1.getT1());
return map;
}
}
Map<String, Object> map = new RetClazz().returnVal(r4);
Object result = map.get("nullObject");
if (result instanceof R2) {
printR2null("methodReturn");
return;
}
System.out.println("methodReturn->" + map.get("result"));
}
你又搞什么鬼,返回值怎么变成对象了?不过好歹比前面的方法看着舒服点,就这样吧,以后需求变动就让你改。
do while (false) 再次出场
private static void dowhile2(R4 r4) {
String result = null;
do {
if (r4 == null) {
break;
}
R3 r3 = r4.getR3();
if (r3 == null) {
break;
}
R2 r2 = r3.getR2();
if (r2 == null) {
printR2null("dowhile");
return;
}
R1 r1 = r2.getR1();
if (r1 == null) {
break;
}
result = r1.getT1();
} while (false);
System.out.println("dowhile->" + result);
}
我是卖人体器官的,有张脸,你要吗?既没有套娃又没有定义多余对象,就这么实现了? 有人公然贩卖人体器官?报警吧。 先跑吧,我这张老脸可不能让你拿了去,不然没脸见人了。
|