zoukankan      html  css  js  c++  java
  • groovy函数、字符串、循环

    三重单引号字符串
      '''a triple single quoted string''' 三重单引号字符串是普通的java.lang.String

    三重单引号字符串是多行的。您可以跨越行边界跨越字符串的内容,而无需将字符串拆分为多个部分,而不使用连接或换行转义字符

    def aMultilineString = '''line one
        line two
        line three'''

    双引号字符串

      Double quoted strings are plain java.lang.String if there’s no interpolated expression, but are groovy.lang.GString instances if interpolation is present.

    字符串插值

      除了单引号和三引号字符串之外,任何Groovy表达式都可以在所有字符串文字中进行插值。插值是在对字符串求值时将字符串中的占位符替换为其值的行为。

    groovy循环
      定义一个独立的repeat函数
      方法体就是一个for循环

    groovy脚本形式代码

    /**
     * Created by Jxy on 2018/12/19 10:32
     * 定义函数
     * 字符串
     * 字符串插值
     */
    
    println function()
    /*
    定义函数
     */
    def function(){
        "groovy"
    }
    //字符串插值
    def name = 'Guillaume' // a plain string
    def greeting = "Hello ${name}"
    
    assert greeting.toString() == 'Hello Guillaume'
    
    def sum = "The sum of 2 and 3 equals ${2 + 3}"
    assert sum.toString() == 'The sum of 2 and 3 equals 5'
    
    def person = [name: 'Guillaume', age: 36]
    assert "$person.name is $person.age years old" == 'Guillaume is 36 years old'
    
    //GString and String hashCodes
    //GStrings and Strings don’t have the same hashCode.
    assert "one: ${1}".hashCode() != "one: 1".hashCode()
    
    /*
    默认参数值
     */
    def repeat(val, repeat=5){
        for(i in 0..<repeat){
            println val
        }
    }
    
    repeat("jser",3)//循环三次
    repeat("good day")//循环默认值五次

    运行结果

    groovy
    jser
    jser
    jser
    good day
    good day
    good day
    good day
    good day
    
    Process finished with exit code 0
  • 相关阅读:
    一些python练习题
    爬取某房源网数据
    练习1:定义 is_Even 函数,传一 int 参数,判断是否是偶数,return True;不是,return False
    pycharm中tkinter 不显示窗口
    保护眼睛颜色的RGB数值
    Python study----------class的实例化
    HTML----------STUDY
    Python study----------字典 popitem()
    Python study----------Python搜索路径
    Python study----------第三方module-----chardet(识别编码)
  • 原文地址:https://www.cnblogs.com/jsersudo/p/10147581.html
Copyright © 2011-2022 走看看