Groovy mistakenly using constructor of enclosing class?

若如初见. 提交于 2019-12-12 17:50:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!