安装环境:
1、官网下载安装
2、mac可使用homebrew工具一键安装
$ brew install groovy
#检查版本 $ groovy -v #输出一下表示安装成功: #Groovy Version: 3.0.9 JVM: 1.8.0_301 Vendor: Oracle Corporation OS: Mac OS X
新建一个 Groovy 文件 hello.groovy
#终端运行Groovy文件 groovy hello.groovygroovy基本语法
//运行:groovy hello.groovy
class Example{
static main(String[] args) {
//基本输入输出、引号区分
//baseOutputFun()
//注释
//单行注释
//数据类型、标识符
//DataTypeFun()
//函数调用
//PrintHello();
//println(sum(5, 50));
//条件语句
//ProvisoFun()
//循环语句
//LoopFun()
//正则表达式
//RegexFcn()
def text ='This Tutorial focuses on $TutorialName. In this tutorial you will learn about $Topic'
def binding = ["TutorialName":"Groovy", "Topic":"Templates"]
def engine = new groovy.text.SimpleTemplateEngine()
def template = engine.createTemplate(text).make(binding)
println template
}
static def baseOutputFun(){
//单引号和双引号都OK,但是单引号只是单纯的字符串,不能使用表达式、运算、求值、正则等
println "hello world"
println 'hello world'
def name='yeira'
println "双引号内输出变量:${name},表达式:${1+2}"
println '单引号内不能输出表达式:${name},表达式:${1+2}'
//三引号也一样,
println """
name:
${name}
"""
println'''
name:
${name}
'''
//可以不使用分号
println "使用分号输出";
println '没有分号'
}
static def DataTypeFun(){
String str = "Hello"; // 字符串
int i = 5; // 整数
long l = 100L; // 长整型
float f = 10.56f; // 32位浮点数
double d = 10.5e40; // 64位浮点数
char ch = 'A'; // 字符
Boolean bl = true; // 布尔值,可以是true或false。
println(str);
println(i);
println(l);
println(f);
println(d);
println(ch);
println(bl);
//def是在groovy中用来定义标识符的关键字,def会根据变量值来判断类型
def name
name='yeira'
println "${name.getClass()}"//class java.lang.Integer
//弱类型语言,改变变量n的类型
def n=1
println "${n.getClass()}"//class java.lang.Integer
n="123"
println "${n.getClass()}"//class java.lang.String
Integer x = 5,y = 10,z = 0;
z = x+y;
println(z);//15
int a=5,b=10,c=0;
c=a+b;
println(c)//15
//字符串
//字符串索引
String str_index="hello world"
println ("str_index[1]: "+str_index[1])
println ("str_index[-1]: "+str_index[-1])//打印字符串的倒数第一个
println ("str_index[1..6]: "+str_index[1..6])//打印索引从1-5
println "str_index[4..2]: ${str_index[4..2]}"//打印从索引4到索引2的字符串,倒序输出
//字符串拼接
str_index+=' !!'
println "拼接字符串:${str_index}"
//字符串重复 通过'*'运算符完成
println (str_index*3)
//字符串长度
println (str_index.size())
println (str_index.length())
//列表:List 使用的方法同java
List l1 = [11,12,13,14] //整数值列表
List l2 = ['Angular','Groovy','Java'] //字符串列表
List l3 = [1,2,[3,4],5] //嵌套列表
List l4 = ['Groovy',21,2.11] //异构的对象引用列表
List l5 = [] //一个空列表
//映射:映射(也称为关联数组,字典,表和散列)是对象引用的无序集合。
//Map集合中的元素由键值访问。 Map中使用的键可以是任何类。当我们插入到Map集合中时,需要两个值:键和值。
Map map1=['TopicName':'Lists','TopicName':'Maps'] //具有TopicName作为键的键值对的集合及其相应的值。
Map map2=[:] //空映射。
//时间和日期:public Date(); 长毫秒:public Date(long millisec);millisec:毫秒数
Date date=new Date()
System.out.println(date.toString()); //Tue Jan 04 17:41:28 CST 2022
//修改Date对象 以表示自标准基准时间(称为“该历元”,即1970年1月1日,00:00:00 GMT)起指定的毫秒数
date=new Date(1000)
System.out.println(date.toString()); //Thu Jan 01 08:00:01 CST 1970
}
static def PrintHello() {
println("This is a print hello function in groovy");
}
//参数默认值
static int sum(int a, int b, int c = 10) {
int d = a+b+c;
return d;
}
static def ProvisoFun(){
// 初始化变量值
int a = 2
// 条件判断
if (a < 100) {
// 如果a<100打印下面这句话
println("The value is less than 100");
} else {
// 如果a>=100打印下面这句话
println("The value is greater than 100");
}
}
static def LoopFun(){
int count = 0;
println("while循环语句:");
while(count<5) {
println(count);
count++;
}
println("for循环语句:");
for(int i=0;i<5;i++) {
println(i);
}
println("for-in循环语句:");
int[] array = [0,1,2,3];
for(int i in array) {
println(i);
}
println("for-in循环范围:");
for(int i in 1..5) {
println(i);
}
}
static def RegexFcn(){
if('Groovy' ==~ 'oo' )
print '123'
println"""
${'Groovy' =~ 'Groovy' }
${'Groovy' =~ 'oo' }
${'Groovy' ==~ 'Groovy' }
${'Groovy' ==~ 'oo' }
${'Groovy' =~ '∧G' }
${'Groovy' =~ 'G$' }
${'Groovy' =~ 'Gro*vy'}
${'Groovy' =~ 'Gro{2}vy'}
"""
}
}
官方文档:http://groovy-lang.org/syntax.html



