public class JavaManyCases {
public static void main(String[] args) {
System.out.println(Color.getInstance("red").toString());
}
}
class Color{
private String title;
private Color(String title){
this.title = title;
}
private static final Color red = new Color("红色");
private static final Color blue = new Color("蓝色");
private static final Color yellow = new Color("黄色");
public static Color getInstance(String color){
switch (color){
case "red":{
return red;
}
case "blue":{
return blue;
}
case "yellow":{
return yellow;
}
default:return null;
}
}
@Override
public String toString() {
return "Color{" +
"title='" + title + '\'' +
'}';
}
}
如果大家喜欢的可以关注我的微信公众号