Return first non-null value

前端 未结 8 1983
情歌与酒
情歌与酒 2020-12-14 07:17

I have a number of functions:

String first(){}
String second(){}
...
String default(){}

Each can return a null value, except the default. <

8条回答
  •  -上瘾入骨i
    2020-12-14 08:10

    Just make a class with one function like this:

    class ValueCollector {
      String value;
      boolean v(String val) { this.value = val; return val == null; }
    }
    
    ValueCollector c = new ValueCollector();
    if c.v(first()) || c.v(second()) ...
    return c.value;
    

提交回复
热议问题