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中Arrays工具类

    以下是实现六种功能的方法: 1.比较两个数组值是否相等: 结果为true.false.(布尔型不能比较) int []a={10,20,30}; int []b={10,20,30}; int []c ...

  2. 关于一些blog优化

    有很多的好看的$java\ script$ 可以大大的增加$blog$的好看度. 这里,本宝宝就列举几个 upd:不定期更新 1.有木有觉得背景的小姐姐和雪花特效极其的配啊啊啊!!! 页面定制CSS插 ...

  3. jenkins+gitlab+robot framework 中遇到的坑

    问题一:拉Git源代码时提示无权限 原来之前用的ssh密钥一直都是自己的用户生成的.其实在Jenkins系统使用的都是Jenkins这个系统帐号的. 解决方法: 切换到jenkins这个帐号下生成个新 ...

  4. JAVA数组的遍历和取最值

    1.获取数组中的所有元素,会用到数组的遍历 数组的遍历,通常用for循环. public class ArrayDemo { public static void main(String[] args ...

  5. [Objective-C语言教程]类和对象(24)

    Objective-C编程语言的主要目的是为C编程语言添加面向对象,类是Objective-C的核心特性,支持面向对象编程,通常称为用户定义类型. 类用于指定对象的形式,它将数据表示和方法组合在一起, ...

  6. [Swift]遍历集合类型(数组、集合和字典)

    Swift提供了三种主要的集合类型,称为数组,集合和字典,用于存储值集合. 数组是有序的值集合. 集是唯一值的无序集合. 字典是键值关联的无序集合. Swift中无法再使用传统形式的for循环. // ...

  7. 把redhat源换成centos的,解决redhat未注册不能下载相关软件的问题

      修改 vim /etc/yum.repos.d/centos-base.repo如下,清华源地址为  https://mirrors.tuna.tsinghua.edu.cn/help/cento ...

  8. 通过UIColor转换为UIImage

    + (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); ...

  9. angular4套用primeng样式

    首先安装primeng cnpm install primeng --save 这样会在项目目录中增加node_modules\primeng目录 package.json文件增加了以下一行 &quo ...

  10. O01-Linux CentOS7中利用RDO部署OpenStack

    一.前言 1.RDO是红帽Red Hat 的一个开源项目,全称是RPM Distribution of OpenStack,能够帮助我们快捷部署OpenStack项目. 官方部署文档:https:// ...