复杂对象的常规闭包
我如何为下面的复杂场景编写闭包,
def empList=[];
EmployeeData empData = null;
empData=new EmployeeDataImpl("anish","nath");
empList.add(empData );
empData=new EmployeeDataImpl("JOHN","SMITH");
empList.add(empData );
Employee employee= new Employee(empList);
如何编写闭包以便使用 emplyee 对象获得 empData 详细信息?有什么提示想法吗?
How can i write closure for below complex scenario
def empList=[];
EmployeeData empData = null;
empData=new EmployeeDataImpl("anish","nath");
empList.add(empData );
empData=new EmployeeDataImpl("JOHN","SMITH");
empList.add(empData );
Employee employee= new Employee(empList);
how can i write closure so that using emplyee object i will get empData details ? any hint Idea ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的意思...
要迭代
EmployeeData
,你可以使用each
:要查找特定条目,你可以使用
find
:或者你可以使用
findAll
找到所有具有给定姓氏的人:这取决于你想要“闭包”做什么...
编辑
对,在你解释了你的 DSL 之后你想要的,我想出了这个(这将解决给定的问题):
它使用
take()
和drop()
,所以你需要 groovy 1.8.1+希望它有意义!然而,这有点奇怪的语法(使用关键字
add
将字符串添加为 Employee),与其这样,最好提出类似于MarkupBuilder
的东西。代码>通过实施BuilderSupport
I'm not sure what you mean...
To iterate over the
EmployeeData
, you could just useeach
:To find a particular entry, you can use
find
:Or you can find all people with a given surname by using
findAll
:It depends what you want "the closure" to do...
Edit
Right, after you have explained your DSL that you want, I have come up with this (which will solve the given problem):
It uses
take()
anddrop()
, so you will need groovy 1.8.1+Hope it makes sense! It's a bit of an odd syntax however (using the keyword
add
to add the strings as an Employee), and instead of this it might be better to come up with something similar toMarkupBuilder
by implementingBuilderSupport