枚举字符串对象中的“无法应用于...”错误

时间:2019-06-19 21:09:12

标签: java enums

我已经研究并修复了格式,创建了getters和setters以及构造函数,但是它仍然返回一个错误,提示我无法应用我的字符串对象。对于我如何表达我的问题,我深表歉意,但由于我不太了解该问题,也不知道该如何很好地解释,因此我不知道该如何清楚地陈述我的问题。

public class Enum {

public enum DictionaryFields {
    DistinctAdjective1("Distinct", "[adjective]", "Familiar. Worked in Java"),
    DistinctAdjective2("Distinct", "[adjective]", "Unique. No duplicates. Clearly different or of different kind."),
    DistinctAdverb("Distinct", "[adverb]", "Uniquely. Written \"distinctly\"."),
    DistinctNoun1("Distinct", "[noun]", "A keyword in this assignment."),
    DistinctNoun2("Distinct", "[noun]", "An advanced search option."),
    PlaceholderAdjective("Placeholder", "[adjective]", "To be updated...");


}

private final String generalNote = "DICTIONARY 340 JAVA";
private String definition;
private String word;
private String partOfSpeech;

private DictionaryFields(String word, String partOfSpeech, String definition) {
    this.word = word;
    this.partOfSpeech = partOfSpeech;
    this.definition = definition;
}

public String getWord() {
    return word;
}

public void setWord {
    this.word = word;
}

public String getPartOfSpeech() {
    return partOfSpeech;
}

public void setPartOfSpeech() {
    this.partOfSpeech = partOfSpeech;
}

public String getDefinition() {
    return definition;
}

public void setDefinition(String definition) {
    this.definition = definition;
}

}

2 个答案:

答案 0 :(得分:1)

您的代码存在一些语法问题。具体来说,您正在定义一个名为“ Enum”的类,但实际上您应该定义一个枚举,例如:

public enum DictionaryFields {
    DistinctAdjective1("Distinct", "[adjective]", "Familiar. Worked in Java"),
    DistinctAdjective2("Distinct", "[adjective]", "Unique. No duplicates. Clearly different or of different kind."),
    DistinctAdverb("Distinct", "[adverb]", "Uniquely. Written \"distinctly\"."),
    DistinctNoun1("Distinct", "[noun]", "A keyword in this assignment."),
    DistinctNoun2("Distinct", "[noun]", "An advanced search option."),
    PlaceholderAdjective("Placeholder", "[adjective]", "To be updated...");



    private final String generalNote = "DICTIONARY 340 JAVA";
    private String definition;
    private String word;
    private String partOfSpeech;

    private DictionaryFields(String word, String partOfSpeech, String definition) {
        this.word = word;
        this.partOfSpeech = partOfSpeech;
        this.definition = definition;
    }

    public String getWord() {
        return word;
    }

    public void setWord() {
        this.word = word;
    }

    public String getPartOfSpeech() {
        return partOfSpeech;
    }

    public void setPartOfSpeech() {
        this.partOfSpeech = partOfSpeech;
    }

    public String getDefinition() {
        return definition;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }

}

您需要在“ DictionaryFields”内部定义构造函数。设置它们的方式是实际上是为“ Enum”类定义一个构造函数。

答案 1 :(得分:0)

您的枚举的构造函数不在枚举的范围内。问题与字段和获取器/设置器相同。将它们移到枚举块中就可以了。