-1
如何从groovy字符串引用对象?我有很多对象 - reviewSyd1,reviewSyd2..reviewSyd30,我需要添加到对象 - 悉尼。我有循环从字符串groovy引用对象
for(i in 1..30){
sydney.addToReviews("reviewSyd$i")
}
这是行不通的。如何使其工作?
谢谢
更新澄清(对不起,如果我不清楚)
class Review{
String email
String Content
}
def rev1 = new Review(email:'[email protected]',content:'i really like it')
def rev2 = new Review(email:'[email protected]',content:'i really like it')
def rev3 = new Review(email:'[email protected]',content:'i really like it')
def rev4 = new Review(email:'[email protected]',content:'i really like it')
for(i in 1..4){
println rev$i.email
}
如何引用对象-revX其中x是迭代器?
您不必对这些对象的引用? – doelleri
这些对象不在集合中?如果它们在脚本中,则可通过'binding'变量访问它们 – Will