我正在开发一个依赖于'字段'中的值的Groovy脚本。继承自Java。我需要能够引用这个字段列表(和值)来创建一个可以使用的地图。我正在努力与一个常规的'这样做的方式。
我有权访问(继承):
dialogPartyASelection_7 = 'Denied'
dialogPartyBSelection_7 = 'Accepted'
dialogPartyASelection_6 = 'Denied'
dialogPartyBSelection_6 = 'Accepted'
dialogPartyBSelection_5 = 'Denied'
dialogPartyASelection_5 = 'Accepted'
dialogPartyBSelection_4 = 'Denied'
dialogPartyASelection_4 = 'Accepted'
dialogPartyBSelection_3 = 'Denied'
dialogPartyASelection_3 = 'Accepted'
dialogPartyBSelection_2 = 'Denied'
dialogPartyASelection_2 = 'Accepted'
dialogPartyBSelection_1 = 'Denied'
dialogPartyASelection_1 = 'Accepted'
我想要得到的东西:
map = [
'dialogPartyASelection_7' : 'Denied',
'dialogPartyBSelection_7' : 'Accepted',
'dialogPartyASelection_6' : 'Denied',
'dialogPartyBSelection_6' : 'Accepted',
'dialogPartyBSelection_5' : 'Denied',
'dialogPartyASelection_5' : 'Accepted',
'dialogPartyBSelection_4' : 'Denied',
'dialogPartyASelection_4' : 'Accepted',
'dialogPartyBSelection_3' : 'Denied',
'dialogPartyASelection_3' : 'Accepted',
'dialogPartyBSelection_2' : 'Denied',
'dialogPartyASelection_2' : 'Accepted',
'dialogPartyBSelection_1' : 'Denied',
'dialogPartyASelection_1' : 'Accepted' ]
我自我设定'如下(这些列表将在整个脚本中重复使用)
def selectionsPartyA = [], selectionsPartyB = [], selections = [], PostSelections = [], PostChildrenSelections = [],
PostSelections = [
'7',
'6',
'5',
'4',
'3',
'2',
'1'
]
// selectionsPartyA
for (post in PostSelections) {
selectionsPartyA += "dialogPartyASelection_"+post
}
// selectionsPartyB
for (post in PostSelections) {
selectionsPartyB += "dialogPartyBSelection_"+post
}
selections.addAll(selectionsPartyB)
selections.addAll(selectionsPartyA)
我已尝试使用我所拥有的参考列表进行迭代,但无法引用'字段'或者'价值'如预期的那样。
答案 0 :(得分:2)
class Test {
String dialogPartyASelection_7 = 'Denied'
String dialogPartyBSelection_7 = 'Accepted'
String dialogPartyASelection_6 = 'Denied'
String dialogPartyBSelection_6 = 'Accepted'
String dialogPartyBSelection_5 = 'Denied'
String dialogPartyASelection_5 = 'Accepted'
String dialogPartyBSelection_4 = 'Denied'
String dialogPartyASelection_4 = 'Accepted'
String dialogPartyBSelection_3 = 'Denied'
String dialogPartyASelection_3 = 'Accepted'
String dialogPartyBSelection_2 = 'Denied'
String dialogPartyASelection_2 = 'Accepted'
String dialogPartyBSelection_1 = 'Denied'
String dialogPartyASelection_1 = 'Accepted'
}
def obj = new Test()
def propMap = obj.properties as Map
assert propMap['dialogPartyASelection_5'] == 'Accepted'
答案 1 :(得分:0)
感谢您的帮助,@ Steinar建议我能够像下面这样做:
resources :posts do
resources :pushs do
resources :reviews
end
end