13.控制结构

1       控制结构

1.1  assert,在Groovy 中叫做Groovy truth

只有断言Boolean与java相同。

空集合或者null是false。0断言为false,其他数字断言为true。

package assert1

 

class AssertTest {

 

    static main(args) {

       //空集合或null断言为false

       def map = [:];

       assert !map;

       def test = null;

       assert !test;

      

       def list = ["Ubuntu","Android"];

       assert list;

      

       //0断言为false,其他数字断言为true。

       assert !0;

       assert 1;

       assert -1;

      

       assert "Hello";

      

    }

 

}

 

 

assert,在Groovy 中叫做Groovy truth。

1.2  if 语句块

支持if and switch 语句,if语句支持断言,例如,在if中,你使用list作为参数,断言该list。

1.3  switch 语句块和isCase方法

switch语句非常灵活,任何实现了 isCase 方法的类型都能被使用。groovy提供了一个 isCase 方法,使用isInstance的实现:

如果是对象object,使用equals;

如果是Collections,使用contains;

如果是正则表达式,使用matches。

你也能指定一个闭包,来校验一个Boolean值。

package structure

 

class SwitchTest {

 

    def testingSwitch(input) {

       def result

       switch (input) {

           case 51://对象

              result = 'Object equals'

              break

           case ~/^Regular.*matching/://正则表达式

              result = 'Pattern match'

              break

           case 10..50://区间

              result = 'Range contains'

              break

           case ["Ubuntu", 'Android', 5, 9.12]://集合

              result = 'List contains'

              break

           case { it instanceof Integer && it < 50 }://闭包

              result = 'Closure boolean'

              break

           case String://String

              result = 'Class isInstance'

              break

           default:

              result = 'Default'

              break

       }

       result;

    }

   

    def test() {

       assert 'Object equals' == testingSwitch(51)

       assert 'Pattern match' == testingSwitch("Regular pattern matching")

       assert 'Range contains' == testingSwitch(13)

       assert 'List contains' == testingSwitch('Ubuntu')

       assert 'Closure boolean' == testingSwitch(9)

       assert 'Class isInstance' == testingSwitch('This is an instance of String')

       assert 'Default' == testingSwitch(200)

    }

    static main(args) {

       SwitchTest t = new SwitchTest();

       t.test();

    }

}

 

如果有多个条件都适合,则第一个case 块被处理。

如果想在switch语句块中,使用自定义的class,只需要实现isCase 方法即可。

1.4  ?.空值处理

当你访问一个对象的某个属性的时候,如果该属性为空值,则会抛出NullPointerException 异常。解决方法:使用?. 

package structure

 

class NullOperator {

    def firstName;

    static main(args) {

       NullOperator obj = null;

       def firstName = obj.firstName;

       println firstName;

    }

}

输出

Caught: java.lang.NullPointerException

java.lang.NullPointerException

    at structure.NullOperator.main(NullOperator.groovy:7)

 

package structure

 

class NullOperator {

    def firstName;

    static main(args) {

       NullOperator obj = null;//(1)

       def firstName = obj?.firstName;

       println firstName;

    }

}

 

输出

null

如果将上例(1)处,改成NullOperator obj = new NullOperator(firstName:"左运松");

则输出:左运松

1.5  ?:操作符

?:是很短的java的三元操作符。如果一个表达式的结果是false或者null时,使用它来设置一个默认值。

package structure

 

class ThreeOperator {

 

    static main(args) {

       //如果一个对象存在,则返回它;否则,新建一个对象并返回。

       //for groovy

       String test = null;

       String result2 = test?:new String();

       //for java

       String result3 = (test!=null)?test:new String();

      

    }

 

}

 

posted @ 2016-07-29 17:32  遥远2  阅读(116)  评论(0编辑  收藏  举报