首页 技术 正文
技术 2022年11月21日
0 收藏 500 点赞 4,732 浏览 2343 个字
public class TestEnum {
    /*最普通的枚举*/
    public enum ColorSelect {
        red, green, yellow, blue;    
    }    /* 枚举也可以象一般的类一样添加方法和属性,你可以为它添加静态和非静态的属性或方法,这一切都象你在一般的类中做的那样. */
    public enum Season {
        // 枚举列表必须写在最前面,否则编译出错
        winter, spring, summer, fall;        private final static String location = "Phoenix";                public static Season getBest() {
            if (location.equals("Phoenix"))
                return winter;
            else
                return summer;
        }
    }
    /*还可以有构造方法*/
    public enum Temp {
        /*通过括号赋值,而且必须有带参构造器和一属性跟方法,否则编译出错
         * 赋值必须是都赋值或都不赋值,不能一部分赋值一部分不赋值
         * 如果不赋值则不能写构造器,赋值编译也出错*/
        absoluteZero(-459), freezing(32),boiling(212), paperBurns(451);
        
        private final int value;
        public int getValue() {
            return value;
        }
        //构造器默认也只能是private, 从而保证构造函数只能在内部使用
        Temp(int value) {
            this.value = value;
        }
    }    public static void main(String[] args) {
        /*
         * 枚举类型是一种类型,用于定义变量,以限制变量的赋值 赋值时通过"枚举名.值"来取得相关枚举中的值
         */
        ColorSelect m = ColorSelect.blue;
        switch (m) {
        /*注意:枚举重写了ToString(),说以枚举变量的值是不带前缀的
          *所以为blue而非ColorSelect.blue
          */
   case red:
            System.out.println("color is red");
            break;
        case green:
            System.out.println("color is green");
            break;
        case yellow:
            System.out.println("color is yellow");
            break;
        case blue:
            System.out.println("color is blue");
            break;
        }
        System.out.println("遍历ColorSelect中的值");
        /*通过values()获得枚举值的数组*/
        for (ColorSelect c : ColorSelect.values()) {
            System.out.println(c);
        }  
   System.out.println("枚举ColorSelect中的值有:"+ColorSelect.values().length+"个");
   /*ordinal()返回枚举值在枚举中的索引位置,从0开始*/
  System.out.println(ColorSelect.red.ordinal());//0
  System.out.println(ColorSelect.green.ordinal());//1
  System.out.println(ColorSelect.yellow.ordinal());//2
  System.out.println(ColorSelect.blue.ordinal());//3  /*枚举默认实现了java.lang.Comparable接口*/ 
  System.out.println(ColorSelect.red.compareTo(ColorSelect.green));  System.out.println(Season.getBest());
        
        for(Temp t:Temp.values()){
            /*通过getValue()取得相关枚举的值*/
            System.out.println(t+"的值是"+t.getValue());
        }    }
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294