Java 枚举字符串对象中出现“无法应用于…”错误
我已经研究并修复了格式,创建了getter、setter和constructor,但仍然返回一个错误,我无法应用我的字符串对象。我为我的问题的措辞道歉,但我不知道如何清楚地陈述我的问题,因为我不太理解它,也不知道如何解释它Java 枚举字符串对象中出现“无法应用于…”错误,java,enums,Java,Enums,我已经研究并修复了格式,创建了getter、setter和constructor,但仍然返回一个错误,我无法应用我的字符串对象。我为我的问题的措辞道歉,但我不知道如何清楚地陈述我的问题,因为我不太理解它,也不知道如何解释它 public class Enum { public enum DictionaryFields { DistinctAdjective1("Distinct", "[adjective]", "Familiar. Worked in Java"), Dis
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;
}
}您的代码存在一些语法问题。具体来说,您正在定义一个名为Enum的类,但实际上应该定义一个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类定义构造函数。您的Enum构造函数不在Enum的范围内。字段和getter/setter的问题也是如此。将它们移动到enum的块中,就可以了。去掉第一行,还有大约10行的}字符。在单独的文件中创建enum,不要将所有内容压缩到一个文件中,错误由@DawoodibnKareemTLDR提到:您在错误的位置定义了构造函数。您也不需要名为Enum的包装器类