开发者

Java enum constructor error

I'm trying to create an enum class so that I can use each value in a for loop, getting each value one at a time.

This is my enum declaration:

public enum SpaceStyle {

    public SpaceStyle(spaceStyle1, spaceStyle2, spaceStyle3);
    private String key;

    SpaceStyle(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }
}

This is the for loop where I want to loop and use each method.

for (SpaceStyle key : SpaceStyle.values()) {
    Map<String, String> contentMap = createContentMap(conf, versionNumber, data);
    createAndPopulateSpace(conf, versionNumber, contentMap, key.toString());
}

// The final method where the ke开发者_如何学编程y of the enum will be used.
public StudioResponse createSpace(ProductInstance conf, VersionNumber \
            versionNumber, String spaceName, String spaceKey, String key) {
    return conf.buildRequest("/createspace.action")
        .setArg("name", spaceName)
        .setArg("key", spaceKey)
        .setArg("permissionSetter.registeredCanView", "true")
        .setArg("permissionSetter.registeredCanEdit", "true")
        .setArg("themeKey", "com.atlassian.studio.confluence" + key)
        .execute("Creating space '" + spaceName + "'");
    }
}


That won't compile, because you can't have a public constructor in an enum.

This will compile:

public enum SpaceStyle {
    spaceStyle1("some-key"),
    spaceStyle2("some-other-key"),
    spaceStyle3("foo-bar");

    private String key;

    private SpaceStyle(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜