import java.util.regex.Matcher

/**
* Created by Jxy on 2018/12/20 10:29
* groovy运算符
*/
/*class operator {
static void main(String ...args){
assert -(-1) == 1
assert (!'foo') == false
assert (!'') == true } }*/
class User {
public final String name
User(String name) { this.name = name}
String getName() { "Name: $name" }
}
def user = new User('jser')
assert user.name == 'Name: jser'
/*
use of .@ forces usage of the field instead of the getter
*/
assert user.@name == 'Bob'
println(user.@name) /*
方法指针
重载方法
*/
def doSomething(String str ){
str.toUpperCase()
}
def doSomething(Integer integer){
integer*2
} def action = this.&doSomething(24)
println(action) /*
查找运算符
*/
def text = "some text to match"
def m = text =~ /match/
assert m instanceof Matcher
if (!m) {
throw new RuntimeException("Oops, text not found!")
} m = text ==~ /match/
assert m instanceof Boolean
/*
操作符(*.)
*/
class Car {
String make
String model
}
def cars = [
new Car(make: 'Peugeot', model: '508'),
new Car(make: 'Renault', model: 'C00o')]
def makes = cars.collect{ it.make }//cars*.make
assert makes == ['Peugeot', 'Renault'] /*
传递操作符(*)
*/
int function(int x, int y, int z) {
x*y+z
}
def list = [5,6,7]
def args = [4]
println(function(4,5,*args))
println(function(*list)) /*
范围运算符
*/
def range = 0..5
assert range.collect() == [0, 1, 2, 3, 4, 5]
assert (0..<5).collect() == [0, 1, 2, 3, 4]
assert (0..5) instanceof List
assert (0..5).size() == 6

groovy运算符的更多相关文章

  1. 【Groovy基础系列】 Groovy运算符

    ?运算符 在java中,有时候为了避免出现空指针异常,我们通常需要这样的技巧: if(rs!=null){ rs.next() … … } 在groovy中,可以使用?操作符达到同样的目的: rs?. ...

  2. groovy–运算符重载

    Groovy支持运算符重载,各种运算符被映射到普通的java对象的方法调用,这就使得开发者可以利用运算符重载的优势来编写自己的Java或者groovy对象. 下面的表格描述了groovy中的操作符所映 ...

  3. Groovy基本类型与运算符

    字符串 1.1字符串段落 def s = """Groovy Grails JAVA """ 输出结果: Groovy Grails JAV ...

  4. 看懂Gradle脚本(4)- Groovy语法之运算符重载

    继续讨论Task定义 回想一下前一篇文章的样例: task myTask { doLast { println 'hello world!' } } 这段脚本定义了一个名为myTask的任务.而且通过 ...

  5. groovy实现循环、交换变量、多赋值、?.运算符

    /** * Created by Jxy on 2019/1/3 10:01 * 1.实现循环的方式 * 2.安全导航操作符---?. * 3.一次性赋值给多个变量 */ 0.upto(2){ pri ...

  6. Groovy入门经典 随书重点

    1 数值和表达式 1.1数值 整数是Integer类的实例 有小数部分的数值是BigDecimal类的实例 不同于java,没有基础数据类型 一切皆对象的概念重于java 1.2表达式 两个整数的除法 ...

  7. Groovy入门教程

    Groovy入门教程 kmyhy@126.com  2009-5-13 一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中. 作为跑在JVM ...

  8. atitit groovy 总结java 提升效率

    atitit groovy 总结java 提升效率 #---环境配置 1 #------安装麻烦的 2 三.创建groovy项目 2 3.  添加 Groovy 类 2 4.  编译运行groovy类 ...

  9. Groovy新手教程

    Groovy新手教程 kmyhy@126.com  2009-5-13 一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也执行在 JVM 中. 作为跑在JVM ...

随机推荐

  1. 封装继承多态-java面向对象三大特征

    第七章  封装和继承 一.面向对象的三大特征 封装.继承.多态 二.封装 将类的属性(字段)设置为私有的(private),并添加getter/setter方法操作属性. 注意:get|set方法的固 ...

  2. Python面向对象(self参数、封装)

    day24 面向对象三大特性:封装 self参数 class Bar: def foo(self, arg): print(self, arg) x = Bar() print(x) x.foo(11 ...

  3. LG的nexus5(32GB版本 - 821)-TOT-底包 可用于救砖!

    LG的nexus5(32GB版本 - 821)-TOT-底包 底层修复效果完美,通过LGflashTool1.8直接刷进去就行~ 底包下载: https://pan.baidu.com/s/1Z5WD ...

  4. [转] Cisco路由器DNS配置

    禁用Web服务 Cisco路由器还在缺省情况下启用了Web服务,它是一个安全风险.如果你不打算使用它,最好将它关闭.举例如下: Router(config)# no ip http server 配置 ...

  5. [性能分析]linux文件描述符

    1.什么是文件和文件描述符 Linux中文件可以分为4种:普通文件.目录文件.链接文件和设备文件.1.普通文件是用户日常使用最多的文件,包括文本文件.shell脚本.二进制的可执行和各种类型的数据.l ...

  6. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...

  7. npm 常用配置

    npm config list/ls 显示配置信息npm config list/ls -l 更详细npm -h 显示帮助信息,建议多查看npm -l display full usage info ...

  8. Junit 测试exception

    有两种方法: 一.使用ExpectedException 仅在junit4.7以上支持.不仅可以测试捕获到某异常,也可以测试异常message. 使用例子如下: @Rule public Expect ...

  9. pytest+jenkins安装+allure导出报告

    环境安装: windows7+64位 pytest:4.0.2 allure的安装:allure的python库pytest-allure-adaptor jenkins的安装:2.138.2 JDK ...

  10. 关于ubuntu环境下gcc使用的几点说明

    sudo apt-get build-dep gcc //安装gcc编译器 /* 假设已经创建hello.c文件 */ //方法一 $gcc hello.c //将源文件直接编译成文件名为a.out的 ...