专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

gradle:grovvy(编辑器、基础语法、闭包)

1、grovvy编辑器

(1)在IDEA中打开grovvy编辑器:

84_1.png

(2)grovvy编辑器

84_2.png

(3)运行

84_3.png

执行结果:

84_4.png

2、grovvy基础语法

y(1)语句后的分号可省略:

println("hello world!")

执行结果:

> println("hello world!")
hello world!

(2)可以省略括号:但是println后面需要加空格

println "hello world!"

执行结果:

> println "hello world!"
hello world!

(3)定义变量

数字

def a=8
println a

执行结果:

> def a=8
> println a
8

字符串

def是弱类型的,会根据情况自动赋予类型

def str="hello"
println str

执行结果:

> def str="hello"
> println str
hello

集合类型:

def list=['a','b']
list << 'c'

执行结果:

> def list=['a','b']
> list << 'c'

Result: [a, b, c]

要注意在 << 前后添加空格

集合类型获取元素:

def list=['a','b']
println list.get(1)

取出第二个元素:

> def list=['a','b']
> println list.get(1)
b

map

取值:

def map=['1':'zhai','2':'zhang']
println map.get('1')

执行结果:

> def map=['1':'zhai','2':'zhang']
> println map.get('1')
zhai

添加:

def map = ['1':'zhai','2':'zhang']
map.liu='liu'

执行结果:

> def map = ['1':'zhai','2':'zhang']
> map.liu='liu'

Result: liu

3、grovvy中的闭包

在grovvy中,主要把闭包当参数来使用

(1)闭包的使用

定义一个闭包:

def b1={
    println "hello world"
}

定义一个方法,里面使用闭包类型的参数:

def test(Closure closure){
    closure()
}

调用方法,该方法的参数是闭包类型的:

test(b1)

运行结果:

> def b1={
>     println "hello world"
> }
> def test(Closure closure){
>     closure()
> }
> 
> test(b1)
hello world

(2)带参数的闭包

定义一个带参数的闭包:

def test = {
    v ->
        println "hello ${v}"
}

定义一个方法,参数为闭包类型:

def method(Closure closure){
    closure("world")
}

调用方法:

method(test)

执行结果:

> def test = {
>     v ->
>         println "hello ${v}"
> }
> def method(Closure closure){
>     closure("world")
> }
> method(test)
hello world

文章永久链接:https://tech.souyunku.com/23051

未经允许不得转载:搜云库技术团队 » gradle:grovvy(编辑器、基础语法、闭包)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们