classCarimplementsGroovyInterceptable{ def check(){ System.out.println("check called ...") } def start(){ System.out.println("start called ...") } def drive(){ System.out.println "drive called" } def invokeMethod(String name, args){ System.out.println "Called to $name intercepted" if (name != 'check'){ System.out.print('running filter') Car.metaClass.getMetaMethod('check').invoke(this,null) } def validMethod = Car.metaClass.getMetaMethod(name,args) if (validMethod != null){ validMethod.invoke(this,args) }else{ Car.metaClass.invokeMethod(this,name,args) } } }
car = new Car() car.start() car.drive() car.check() try { car.speed() }catch (e){ println e }
1 2 3 4 5 6 7 8 9 10 11 12 13
Called to start intercepted running filtercheck called ... start called ... Called to drive intercepted running filtercheck called ... drive called Called to check intercepted check called ... Called to speed intercepted running filtercheck called ... groovy.lang.MissingMethodException: No signature of method: Car.speed() is applicable for argument types: () values: [] Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), split(groovy.lang.Closure), check(), start(), inspect()
def first = new VendorWithCtor('canoo','ULC') // Normal constructor use def second = ['Canoo', 'ULC'] as VendorWithCtor // Coercion with as VendorWithCtor third = ['Canoo', 'ULC'] // Corecion in assignment
命名参数
1 2 3 4 5 6 7 8 9 10
classVendor{ String name, product } new Vendor() new Vendor(name:'Canoo') new Vendor(product:'ULC') new Vendor(name:'Canoo', product:'ULC')
def vendor = new Vendor(name:'Canoo') assert'Canoo' == vendor.name
隐式构造函数
1 2 3 4
java.awt.Dimension area area = [200,100] assert area.width == 200 assert area.height == 100
def bean = new MrBean(firstName:'Rowan') bean.lastName = 'Atkinson'
//advanced accessors with groovy
classDoubleBean{ public value //visible value void setValue(value){ this.value = value //inner field access } def getValue(){ value * 2//inner field access } }
def bean2 = new DoubleBean(value:100) assert200 == bean2.value //Property access use getter method assert100 == bean2.@value// Outer field access directly access
classBook { String name List authors } classAuthor { String name Address addr } classAddress { String province String city }
def addr1 = new Address(province:'Guangdong',city:'Shenzhen') def addr2 = new Address(province:'Gunagdong',city:'Guangzhou') def addr3 = new Address(province:'Hunan',city:'Changsha') def addr4 = new Address(province:'Hubei',city:'Wuhan') def books = [new Book(name:'A glance at Java',authors:[new Author(name:'Tom',addr:addr1)]), new Book(name:'Deep into Groovy',authors:[new Author(name:'Tom',addr:addr1),new Author(name:'Mike',addr:addr3)]), new Book(name:'A compare of Struts and Grails',authors:[new Author(name:'Wallace',addr:addr4),new Author(name:'Bill',addr:addr2)]), new Book(name:'learning from Groovy to Grails',authors:[new Author(name:'Wallace',addr:addr3)])]
//目标是找到作者是"Tom"的书籍的书名 //Java风格 def booksOfTomOldWay = [] books.each { def book = it def aus = it.authors aus.each { au-> if (au.name == 'Tom') booksOfTomOldWay << book.name } }