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. [ActionScript 3.0] 模拟win7彩色气泡屏保效果

    主文件: package { import com.views.BubbleView; import com.views.ColorfulBubble; import flash.display.Sp ...

  2. [ActionScript 3.0] as3启动QQ

    import flash.html.HTMLLoader; import flash.net.URLLoader; import flash.net.URLRequest; import flash. ...

  3. 面向对象之ajax

    1.Ajax发送请求的几个步骤 1. 创建 XMLHttpRequest 对象 var xhr = new XMLHttpRequest();//IE6 使用var xhr= new ActiveXO ...

  4. RN47 中通过 JS 调用 Native 方法

    每一个模块.方法都有一个 ID,通过 ID 来调用. m_registry->callNativeMethod(call.moduleId, call.methodId, std::move(c ...

  5. 2016级算法期末模拟练习赛-D.AlvinZH的序列问题

    1111 AlvinZH的序列问题 思路 中等题,动态规划. 简化题意,. 坑点一:二维int数组MLE,明显会超过内存限制,由于\(n\)最大为1e4,那么我们的dp数组最大也是1e4,考虑使用sh ...

  6. Oracle PL/SQL学习之基础篇(1)

    1.PL/SQL,全称Procedure Language/SQL,过程化sql语言 PL/SQL的程序结构 declare --声明部分(包括变量.光标.例外声明) begin --语句序列(DML ...

  7. win10 安装 mysql 5.7

    win10 安装 mysql 5.7 1.在mysql 官网下载mysql 5.7 的手动安装包 mysql--winx64.zip 2. 解压到mysql 目录的文件夹下面 D:\moudle\my ...

  8. [BZOJ 1056][HAOI2008]排名系统

    传送门 \(\color{green}{solution}\) \(fhq \_treap\)模板题. 对于 \(+\) 操作,如果当前人不存在,那么直接加入;如果存在,那么先将他删除,再加入.复杂度 ...

  9. 洛谷 P3757 [CQOI2017]老C的键盘

    题面 luogu 题解 其实就是一颗二叉树 我们假设左儿子小于根,右儿子大于根 考虑树形\(dp\) \(f[u][i]\)表示以\(u\)为根的子树,\(u\)为第\(i\)小 那么考虑子树合并 其 ...

  10. Windows开发经验 - Visual Studio 2017

    1. 调试子进程 Visual Studio 2017及更早的版本原生不支持调试子进程,不确定未来是否会支持.可以通过官方插件让Visual Studio能够调试子进程. https://market ...