慢慢的看《GROOVY IN ACTION》的一个中文节译本,根据上面的东东慢慢练习。
中文看起来确实比英文快好多。。。:)
Book gina = new Book('Groovy in Action')assert gina.getTitle() == 'Groovy in Action'assert getTitleBackwards(gina) == 'noitcA ni yvoorG'String getTitleBackwards(book) { title = book.getTitle() return title.reverse()}def groovyBook = new Book()groovyBook.setTitle('Groovy conquers the world')assert groovyBook.getTitle() == 'Groovy conquers the world'groovyBook.title = 'Groovy conquers the world'assert groovyBook.title == 'Groovy conquers the world'def nick = 'Gina'def book = 'Groovy in Action'assert "$nick is $book" == 'Gina is Groovy in Action'assert '12345' =~ /\d+/assert 'xxxxx' == '12345'.replaceAll(/\d/, 'x')def x = 1def y = 2assert x + y == 3assert x.plus(y) == 3assert x instanceof Integerdef roman = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII']assert roman[4] == 'IV'roman[8] = 'VIII'assert roman.size() == 9def http = [ 100 : 'CONTINUE', 200 : 'OK', 400 : 'BAD REQUEST' ] assert http[200] == 'OK'http[500] = 'INTERNAL SERVER ERROR'assert http.size() == 4x = 1..10assert x.contains(5)assert x.contains(15) == falseassert x.size() == 10assert x.from == 1assert x.to == 10assert x.reverse() == 10..1[1, 2, 3].each { entry -> println entry }def totalClinks = 0def partyPeople = 1001.upto(partyPeople) { guestNumber -> clinksWithGuest = guestNumber - 1 totalClinks += clinksWithGuest}assert totalClinks == (partyPeople * (partyPeople - 1)) / 2if (false) assert falseif (null) { assert false} else { assert true}def i = 0while (i < 10) { i++}assert i == 10def clinks = 0for (remainingGuests in 0..9) { clinks += remainingGuests}assert clinks == (10*9)/2def list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]for (j in list) { assert j == list[j]}list.each() { item -> assert item == list[item]}switch(3) { case 1 : assert false; break case 3 : assert true; break default : assert false}