Groovy笔记5

An assignment is not allowed as a top-level expression in an if test.


The restriction of assignments from being used in top-level Boolean expres-
sions applies only to if and not to other control structures such as while.


三元操作符  expr ? expr : expr


The GDK enhances Class by adding an isCase method that
tests the candidate with isInstance.


The isCase method on closures passes the candidate into the closure and
returns the result of the closure call coerced to a Boolean.


The isCase method on patterns applies its test to the toString value
of the argument.


A classifier is eligible as a switch case if it implements the isCase method.
In other words, a Groovy switch like
    switch (candidate) {
        case classifier1  : handle1()         ; break
        case classifier2  : handle2()         ; break
                          : handleDefault()
        default
    }
is roughly equivalent (beside the fallthrough and exit handling) to
    if      (classifier1.isCase(candidate)) handle1()
    else if (classifier2.isCase(candidate)) handle2()
             handleDefault()
    else



Table 6.2  Implementations of isCase for switch
  Class             a.isCase(b) implemented as

  Object            a.equals(b)
  Class             a.isInstance(b)
  Collection        a.contains(b)
  Range             a.contains(b)
  Pattern           a.matcher(b.toString()).matches()
  String            (a==null && b==null) || a.equals(b)
  Closure           a.call(b)

The isCase method is also used with grep on collections such that
collection.grep(classifier) returns a collection of all items that are
a case of that classifier.



groovy中只有while和for两种循环控制结构


It is more Groovy to treat a string as a collection of characters.


The return keyword is optional for the last expression in a method or
closure. If it is omitted, the return value is that of the last expression.
Methods with explicit return type void do not return a value, whereas closures always
return a value.


Groovy和Java的异常处理机制的最大不同之处在于Groovy允许在方法声明时忽略受控异常的声明


尽管Groovy的其他部分对类型的声明是可选的但是在catch表达式中异常的类型声明是强制的






你可能感兴趣的:(groovy)