问题描述
- Java枚举的使用的问题
-
教材里面有段关于枚举的代码,但一直报错,麻烦高手看一下import java.util. *; public class EnumTest { public static void main(String[] args) { Scanner in=new Scanner(System.in); System.out.print("请输入服装型号:(SMALL,MEDIUM,LARGE,EXTRA_LARGE,EXTRA_EXTRA_LARGE)"); String input=in.next().toUpperCase(); Size size=Enum.valueOf(Size.class,input); System.out.println("Size="+size); System.out.println("abbreviation="+size.getAbbreviation()); if(size==Size.EXTRA_LARGE^size==Size.EXTRA_EXTRA_LARGE) System.out.println("很好,你注意到了下划线"); } public static String abbreviation; enum Size{SMALL,MEDIUM,EXTRA_LARGE,EXTRA_EXTRA_LARGE} private void Size(String abbreviation){this.abbreviation=abbreviation;} public String getAbbreviation(){return abbreviation;} }
解决方案
像这样:
enum Size{SMALL,MEDIUM,EXTRA_LARGE,EXTRA_EXTRA_LARGE;
public String getAbbreviation() {
// TODO Auto-generated method stub
return abbreviation;
}
}
解决方案二:
按你的调用,getAbbreviation方法要定义在枚举中,不是在主类里
时间: 2024-09-20 00:30:01