简体   繁体   English

Java枚举构造函数错误

[英]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. 我正在尝试创建一个enum类,以便可以在for循环中使用每个值,一次获得一个值。

This is my enum declaration: 这是我的enum声明:

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循环。

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

// The final method where the key 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM