java语言中关于枚举的问题,求改错,最好能告诉我原因,谢谢
packagedemo;publicclassPerson5{publicstaticvoidmain(String[]args){colora=newcolor();/...
package demo;
public class Person5
{
public static void main(String[] args){
color a = new color();//创建color
a = color.getlnstance(1);
System.out.println(a.getName());
}
}
class color
{
private String name;//成员变量
public static final color RED = new color("红色");//静态常量
public static final color GREEN = new color("绿色");
public static final color BLUE = new color("蓝色");
public String getName() { return name; }
public void setName(String name) { this.name = name; }
private color(String name) { setName(name); }//构造方法
public static color getlnstance(int i)
{
if (i == 0)
{
return RED;
}
if (i == 1)
{
return GREEN;
}
if (i == 2)
{
return BLUE;
}
return null;
}
} 展开
public class Person5
{
public static void main(String[] args){
color a = new color();//创建color
a = color.getlnstance(1);
System.out.println(a.getName());
}
}
class color
{
private String name;//成员变量
public static final color RED = new color("红色");//静态常量
public static final color GREEN = new color("绿色");
public static final color BLUE = new color("蓝色");
public String getName() { return name; }
public void setName(String name) { this.name = name; }
private color(String name) { setName(name); }//构造方法
public static color getlnstance(int i)
{
if (i == 0)
{
return RED;
}
if (i == 1)
{
return GREEN;
}
if (i == 2)
{
return BLUE;
}
return null;
}
} 展开
2个回答
展开全部
为啥不直接写成枚举呢。
public enum ColorEnum {
RED("RED"),
GREEN("GREEN"),
BLUE("BLUE");
private final String name;
ColorEnum(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public static void main(String[] args){
System.out.println(ColorEnum.RED.getName());
}
}
public enum ColorEnum {
RED("RED"),
GREEN("GREEN"),
BLUE("BLUE");
private final String name;
ColorEnum(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public static void main(String[] args){
System.out.println(ColorEnum.RED.getName());
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询