class Cat { void bark() {println "ニャー!"}; } class Dog { void bark() {println "ワン!"}; } def animal if (new Random().nextInt(2) == 0) { animal = new Dog() } else { animal = new Cat() } animal.bark() class Person { def name int age } 1/2の確立でどちらか のインスタンスが作成 されるので実行する までanimalの型は分 からない
select="SELECT * FROM STAFF WHERE JOB=? AND YEARS > ?" sql.eachRow (select,['Sales',7]) { println "${it.NAME} (JOB=${it.JOB},YEARS=${it.YEARS})" } insert="INSERT INTO TEST3(ID,NAME,JOB,YEARS) VALUES (?,?,?,?)" sql.execute(insert,[100,'KEN','Mgr',10]) sql.execute(insert,[101,'JOE','Sales',5])
ます(ちょっと面倒ですね) ※ヒアドキュメントで記述すれば、コーテーションの前のエスケープ(¥)は省略できます "SELECT ID as id, NAME as name, SALARY as salary FROM ..." class Staff { def id def name def salary } "SELECT ID as ¥"id¥",NAME as ¥"name¥", SALARY as ¥"salary¥" FROM ... "