问题
Given:
static class Question {
// stuff
List<Value> values
static class Value {
// stuff
}
When I run:
Question question = new Question()
question.id = "whatever"
if (it == QuestionType.SELECT || it == QuestionType.MULTICHOICE) {
question.values = [new Question.Value(), new Question.Value()]
for (Question.Value v : question.values) {
// stuff
}
question.values
contains an array of Question objects and NOT Value.
Intellij doesn't give me any error nor warnings. I tried evaluating "new Question.Value()" during debug and it correctly constructed a Value object.
However, if I use a static import:
import static <path>.Question.*
And thus transforming the if
content to:
question.values = [new Value(), new Value()]
for (Value v : question.values) {
// stuff
}
Then I correctly get an array of Value objects!
It doesn't make any sense to me... can anybody enlighten me, please?
Edit:
来源:https://stackoverflow.com/questions/47556253/groovy-mistakenly-using-constructor-of-enclosing-class