zoukankan      html  css  js  c++  java
  • groovy-真值

    Boolean expressions

    Groovy支持标准的条件运算符的布尔表达式:

    1 def a = true
    2 def b = true
    3 def c = false
    4 assert a
    5 assert a && b
    6 assert a || c
    7 assert !c

    此外,Groovy中有强制转换非布尔对象为布尔值的特殊规则。

    集合

    空集合会被强制转换为false:

    1 def numbers = [1,2,3]
    2 assert numbers //true, as numbers in not empty
    3 numbers = []
    4 assert !numbers //true, as numbers is now an empty collection

    迭代器和枚举

    没有进一步元素的枚举和迭代器都会被强制转换为false:

    1 assert ![].iterator() // false because the Iterator is empty
    2 assert [0].iterator() // true because the Iterator has a next element
    3 def v = new Vector()
    4 assert !v.elements() // false because the Enumeration is empty
    5 v.add(new Object())
    6 assert v.elements() // true because the Enumeration has more elements

    Map

    非空的map被强制转换为true:

    1 assert ['one':1]
    2 assert ![:]

    Matchers

    当匹配到正则表达式的模式的时候会强制转换为true:

    1 assert ('Hello World' =~ /World/) //true because matcher has at least one match

    Strings

    非空的Strings, GStrings 和CharSequences 将被强制转换为true:

    1 <div>
    2 <div>
    3 <pre><code data-result="[object Object]">// Strings
    4 assert 'This is true'
    5 assert !''
    6 //GStrings
    7 def s = ''
    8 assert !("$s")
    9 s = 'x'
    10 assert ("$s")

    Numbers

    非0的数值被强制转换为true,就如同perl一样。

    1 <div>
    2 <div>
    3 <pre><code data-result="[object Object]">assert !0 //yeah, 0s are false, like in Perl
    4 assert 1  //this is also true for all other number types

    Object references

    非null的对象引用被强制转换为true:

    1 assert new Object()
    2 assert !null
  • 相关阅读:
    C# Bitmap类型与Byte[]类型相互转化
    我所理解的JavaScript闭包
    从零开始编写自己的C#框架 ---- 系列文章
    巧用Windows 7计划任务设置定时提醒
    vba中MsgBox的参数及用法
    SQL行转列和列转行
    web应用程序 ---- 日志系统的设计
    《代码整洁之道》读书笔记
    使用Excel制作万年历(日历可A4纸打印)
    C#窗体全屏功能
  • 原文地址:https://www.cnblogs.com/rollenholt/p/3349055.html
Copyright © 2011-2022 走看看