groovy学习笔记
groovy学习http://www.groovy-lang.org/documentation.html 安装:1.1,安装gvmtools地址:http://gvmtool.net $ curl -s get.gvmtool.net | bash
$ source "$HOME/.gvm/bin/gvm-init.sh"
$ gvm install groovy
$ groovy -version
1.2 使用二进制包1,下载 2,设置 3,将 4,设置 5,验证 使用命令行:
armytekiMacBook-Air:~ army$ groovysh Groovy Shell (2.4.4,JVM: 1.7.0_45) Type ':help' or ':h' for help. ------------------------------------------------------------------------------- groovy:000> :h For information about Groovy,visit: http://groovy-lang.org groovy:000> class Server {String name} ===> true groovy:000> server = new Server(name:'tt') ===> Server@3fbbaaad groovy:000> println server.getName() tt 界面:
脚本:
shell 脚本方式: #!/usr/bin/env groovy println 'Hello' map=[:] map."a"="1" println map class Person{ String name Person(name){ this.name = name } } def person = new Person(null); println person?.name 与Java比较http://www.groovy-lang.org/differences.html
Groovy的方法在运行时根据实际传入的参数类型调用,在Java中方法在编译时基于声明的类型确定。 int method(String arg) { return 1; } int method(Object arg) { return 2; } Object o = "Object"; int result = method(o); In Java,you would have: assertEquals(2,result); Whereas in Groovy: assertEquals(1,result);
Groovy支持”,”“两种格式的字符串类型,”” 支持 “${}” 插入表达式(称为 GString): assert 'c'.getClass()==String assert "c".getClass()==String assert "c${1}".getClass() in GString 对char类型,单个String字符可以赋给char类型,多个字符通过 // for single char strings,both are the same assert ((char) "c").class==Character assert ("c" as char).class==Character // for multi char strings they are not try { ((char) 'cx') == 'c' assert false: 'will fail - not castable' } catch(GroovyCastException e) { } assert ('cx' as char) == 'c' assert 'cx'.asType(char) == 'c' 对String换行的情况,可以用 throw new PluginException("Failed to execute command list-applications:" + " The group with name " + parameterMap.groupname[0] + " is not compatible group of type " + SERVER_TYPE_NAME) throw new PluginException("Failed to execute command list-applications: The group with name ${parameterMap.groupname[0]} is not compatible group of type ${SERVER_TYPE_NAME}") throw new PluginException("""Failed to execute command list-applications: The group with name ${parameterMap.groupname[0]} is not compatible group of type ${SERVER_TYPE_NAME)}""") 正则表达式,用 def pattern = ~/fo*/ // matches() strings against patterns assert 'foo' =~ pattern assert 'foo' ==~ pattern
Groovy使用Object概念。所有的基本类型也都封装成对象。
程序结构
等于 import org.codehaus.groovy.runtime.InvokerHelper class Main extends Script { def run() { println 'Groovy world!' } static void main(String[] args) { InvokerHelper.runScript(Main,args) } }
当闭包没有使用->显式定义参数,则默认用 def greeting = { "Hello,$it!" } assert greeting('Patrick') == 'Hello,Patrick!' 入参是变长的参数列表时,可以使用 … args def concat1 = { String... args -> args.join('') } assert concat1('abc','def') == 'abcdef'
class Enclosing {
void run() {
def whatIsThisObject = { getThisObject() }
assert whatIsThisObject() == this
def whatIsThis = { this }
assert whatIsThis() == this
}
}
class Enclosing {
void run() {
def whatIsOwnerMethod = { getOwner() }
assert whatIsOwnerMethod() == this
def whatIsOwner = { owner }
assert whatIsOwner() == this
}
}
class Person { String name }
class Thing { String name }
def p = new Person(name: 'Norman')
def t = new Thing(name: 'Teapot')
def upperCasedName = { delegate.name.toUpperCase() }
upperCasedName.delegate = p
assert upperCasedName() == 'NORMAN'
upperCasedName.delegate = t
assert upperCasedName() == 'TEAPOT'
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |