Groovy学习笔记-其他操作符

1.Spreading Operator:扩展操作符 *

class Car{
    String make
    String model
}

def cars = [
    new Car(make:'BenChi',model:'101'),
    new Car(make:'BaoMa', model:'102'),
   null
] def makes = cars*.make println makes /*Output ['BenChi', 'BaoMa', null] */

2扩展操作符 *可以使用在任何实现了Iterable的类中

class Component{
    Long id
    String name
}

class CompositeObject implements Iterable<Component>{
    def components = [
        new Component(id:1, name:'Foo'),
        new Component(id:2, name:'Bar')
    ]
    
    @Override
    Iterator<Component> iterator(){
        components.iterator()
    }
}

def composite = new CompositeObject()
println composite*.id
println composite*.name

/*Output
[1, 2]
[Foo, Bar]

*/

 

上一篇:java-从Groovy连接到AS400的ProgramCallDocument挂起


下一篇:为什么我不能让弹簧加载时间进行编织工作