标签:
class Color{
public static final Color RED = new Color("红色") ; // 定义第一个对象
public static final Color GREEN = new Color("绿色") ; // 定义第一个对象
public static final Color BLUE = new Color("蓝色") ; // 定义第一个对象
private String name ;
private Color(String name){
this.name = name ;
}
public void setName(String name){
this.name = name ;
}
public String getName(){
return this.name ;
}
public static Color getInstance(int i){
switch(i){
case 1:{
return RED ;
}
case 2:{
return GREEN ;
}
case 3:{
return BLUE ;
}default:{
return null ;
}
}
}
};
public class ColorDemo01{
public static void main(String args[]){
Color c1 = Color.RED ; // 取得红色
System.out.println(c1.getName()) ; // 输出名字
Color c2 = Color.getInstance(3) ; // 根据编号取得名字
System.out.println(c2.getName()) ;
}
};
public class SwitchPrintEnum{
public static void main(String args[]){
for(Color c:Color.values()){ // 输出枚举中的全部内容
print(c) ;
}
}
public static void print(Color color){
switch(color){
case RED:{
System.out.println("红颜色") ;
break ;
}
case GREEN:{
System.out.println("绿颜色") ;
break ;
}
case BLUE:{
System.out.println("蓝颜色") ;
break ;
}
default:{
System.out.println("未知颜色") ;
break ;
}
}
}
};
enum Color{
RED,GREEN,BLUE ;
private String name ; // 定义name属性
public void setName(String name){
switch(this){ // 判断操作的是那个枚举对象
case RED:{
if("红色".equals(name)){
this.name = name ; // 允许设置名字
}else{
System.out.println("设置内容错误。") ;
}
break ;
}
case GREEN:{
if("绿色".equals(name)){
this.name = name ; // 允许设置名字
}else{
System.out.println("设置内容错误。") ;
}
break ;
}
case BLUE:{
if("蓝色".equals(name)){
this.name = name ; // 允许设置名字
}else{
System.out.println("设置内容错误。") ;
}
break ;
}
}
this.name = name ;
}
public String getName(){
return this.name ;
}
}
public class ValueOfEnum{
public static void main(String args[]){
Color c = Color.valueOf(Color.class,"BLUE") ; // 得到兰色
c.setName("兰色") ; // 名字错误
c.setName("蓝色") ; // 名字正确
System.out.println(c.getName()) ;
}
};import java.util.EnumMap ;
import java.util.Map ;
enum Color{
RED , GREEN , BLUE ;
}
public class EnumMapDemo{
public static void main(String args[]){
Map<Color,String> desc = null ; // 定义Map对象,同时指定类型
desc = new EnumMap<Color,String>(Color.class) ; // 实例化EnumMap对象
desc.put(Color.RED,"红色") ;
desc.put(Color.GREEN,"绿色") ;
desc.put(Color.BLUE,"蓝色") ;
System.out.println("====== 输出全部的内容 ======") ;
for(Color c:Color.values()){
System.out.println(c.name() + " --> " + desc.get(c)) ;
}
System.out.println("====== 输出全部的键值 ======") ;
for(Color c:desc.keySet()){
System.out.print(c.name() + "、") ;
}
System.out.println() ;
System.out.println("====== 输出全部的内容 ======") ;
for(String s:desc.values()){
System.out.print(s + "、") ;
}
}
};import java.util.EnumSet ;
enum Color{
RED , GREEN , BLUE ;
}
public class EnumSetDemo{
public static void main(String args[]){
EnumSet<Color> esOld = null ; // 声明一个EnumSet对象
EnumSet<Color> esNew = null ;
System.out.println("======== EnumSet.copyOf(Color.class) =====") ;
esOld = EnumSet.noneOf(Color.class) ; // 将枚举的全部类型设置到EnumSet对象之中
esOld.add(Color.RED) ; // 增加内容
esOld.add(Color.GREEN) ; // 增加内容
esNew = EnumSet.copyOf(esOld) ; // 从已有的集合拷贝过来
print(esNew) ;
}
public static void print(EnumSet<Color> temp){ // 专门的输出操作
for(Color c:temp){ // 循环输出EnumSet中的内容
System.out.print(c + "、") ;
}
System.out.println() ;
}
};标签:
原文地址:http://blog.csdn.net/tryitboy/article/details/42884481