zoukankan      html  css  js  c++  java
  • enum 枚举的简单应用

    package com.ibm.enums;
    
    public class TestEnum {
    	
    	public enum ColorEnums{
    		
    		red ,green ,yellow,blue;
    	}
    	
    	public enum Male{
    		
    		ForMale("girl",1),Male("boy",2);
    		
    		private String name;
    		private int index;
    		
    	    public String getName() {
    			return name;
    		}
    
    		public void setName(String name) {
    			this.name = name;
    		}
    
    		public int getIndex() {
    			return index;
    		}
    
    		public void setIndex(int index) {
    			this.index = index;
    		}
    		
    		//// 构造方法
    		private Male(String name,int index){
    	    	
    	    	this.name = name;
    	    	this.index = index ;
    	    }
    	}
    	
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		//遍历枚举
    		for(ColorEnums ce : ColorEnums.values()){  //此处的values 是 变量本身
    			System.out.println(ce);
    		}
    		//赋值时通过“枚举名.值”取得枚举中的值
    		ColorEnums a = ColorEnums.green;
    		switch(a){
    		case red:
    			System.out.println("this is red");
    			break;
    		case green:
    			System.out.println("this is green");
    			break;
    		case yellow:
    			System.out.println("this is yellow");
    			break;
    			
    		}
    		
    		//计算枚举的个数 
    		
    		System.out.println("ColorEnums 枚举个数: "+ ColorEnums.values().length);
    		
    		//计算枚举的index
    		
    		System.out.println(ColorEnums.blue.ordinal()) ; //blue location is 3 ,so it looks like bigan from 0 
    		
    		//枚举方法中实现了 compare 方法
    		
    		System.out.println(ColorEnums.blue.compareTo(ColorEnums.green)); //2 
    		
    		//更复杂的枚举  Male
    		
    		for(Male m : Male.values()){
    			
    			System.out.println(m.index +":"+ m.name); //m.values()  是一个对象 
    			System.out.println();
    		}
    
    	}
    
    }
    


    red
    green
    yellow
    blue
    this is green
    ColorEnums 枚举个数: 4
    3
    2
    1:girl
    2
    2:boy
    2


  • 相关阅读:
    modal 过程中添加动画
    SVSegmentedControl 标签页
    带输入框的UIAlertView
    Django小结
    译Step-by-Step Guide on Configuring Django-Userena
    使用国内镜像通过pip安装python 包
    pycryto实现AES加密解密算法
    notepad++搜索的一些东西
    [转]notepad++正则表达式替换字符串详解
    [转]Notepad++快捷键
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501325.html
Copyright © 2011-2022 走看看