赞
踩
class Person{ String name int age String toString(){ return "${name}:${age}" }}def people = [new Person(name:'阿大',age:24),new Person(name:'阿二',age:18)]println people*.toString()========================result:["阿大:24", "阿二:18"]
String name= person.name?person.name:'unknown'// java的写法def name2= person.name ?: "unknown" // Groovy 的写法
Person userprintln user.name// Throws NullPointerException// 为避免空指针异常,需要加上一个检查,java的写法如下if (user != null) { println "Java FirstName = ${user.firstName}"}// Groovy语法的写法println "Groovy FirstName = ${user?.firstName}"
class Todo { String name def getName() { println "Getting Name" name }}def todo = new Todo(name: "Jim")println todo.nameprintln todo.@name========result:Getting NameJimJim
def list = ['老大','老二','老三']//常规的遍历list.each{ println it}String printName(name){ println name}//将printName作为参数进行遍历list.each(this.&printName)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。