zoukankan      html  css  js  c++  java
  • 枚举

    package com.loaderman.枚举;
    
    public class Demo1_Enum {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            //demo1();
            //demo2();
            //demo3();
            Week3 mon = Week3.TUE;
            switch (mon) {
                case MON:
                    System.out.println("星期一");
                    break;
                case TUE:
                    System.out.println("星期二");
                    break;
            }
        }
    
        public static void demo3() {
            Week3 mon = Week3.MON;
            mon.show();
        }
    
        public static void demo2() {
            Week2 mon = Week2.MON;
            System.out.println(mon.getName());
        }
    
        public static void demo1() {
            Week mon = Week.MON;
            System.out.println(mon);
        }
    
    }
    
    package com.loaderman.枚举;
    
    public class Demo2_Enum {
    
        /**
         * int ordinal()
         * int compareTo(E o)
         * String name()
         * String toString()
         * <T> T valueOf(Class<T> type,String name)
         * values() 
         * 此方法虽然在JDK文档中查找不到,但每个枚举类都具有该方法,它遍历枚举类的所有枚举值非常方便
         */
        public static void main(String[] args) {
            //demo1();
            //    Week2 mon = Week2.valueOf(Week2.class, "MON");    //通过字节码对象获取枚举项
            //    System.out.println(mon);
    
            Week2[] arr = Week2.values();
            for (Week2 week2 : arr) {
                System.out.println(week2);
            }
        }
    
        public static void demo1() {
            Week2 mon = Week2.MON;
            Week2 tue = Week2.TUE;
            Week2 wed = Week2.WED;
          
          /*System.out.println(mon.ordinal());            //枚举项都是有编号的
          System.out.println(tue.ordinal());
          System.out.println(wed.ordinal());
          
          System.out.println(mon.compareTo(tue));          //比较的是编号
          System.out.println(mon.compareTo(wed));*/
    
            System.out.println(mon.name());             //获取实例名称
            System.out.println(mon.toString());          //调用重写之后的toString方法
        }
    
    }
    
    
    package com.loaderman.枚举;
    
    public enum Week2 {
        MON("星期一"),TUE("星期二"),WED("星期三");
    
        private String name;
        private Week2(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    
        public String toString() {
            return name;
        }
    }
    
    
    
    package com.loaderman.枚举;
    
    public enum Week {
        MON,TUE,WED;
    }
    
    package com.loaderman.枚举;
    
    public enum Week3 {
        MON("星期一"){
            public void show() {
                System.out.println("星期一");
            }
        },TUE("星期二"){
            public void show() {
                System.out.println("星期二");
            }
        },WED("星期三"){
            public void show() {
                System.out.println("星期三");
            }
        };
    
        private String name;
        private Week3(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    
        public abstract void show();
    
    }
    
  • 相关阅读:
    (转)在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 定位指定的服务器/实例时出错)
    C++面试题集(最全的C\C++试题集和答案)
    linq
    sqlserver 读取多个表的分页存储过程(参考)
    xml xslt linqxml
    .net 文件操作的一点记录
    HTTP压缩
    图片防盗链
    随笔
    datareader 与 应用程序池
  • 原文地址:https://www.cnblogs.com/loaderman/p/6403416.html
Copyright © 2011-2022 走看看