zoukankan      html  css  js  c++  java
  • [Java Plasterer] Java Components 3:Java Enum

    Writer:BYSocket(泥沙砖瓦浆木匠)

        微博:BYSocket

        豆瓣:BYSocket

    Reprint it anywhere u want.

    Written In The Font

      When we to set some constants for projects, we always use ‘public static final’to set Int or String constants.Or sometimes,we can also set the paramters in properties.When the project starts,we can get the properties to use them.Today,we can use Enum (JDK 1.5).

    Three pieces:
      1. An Example to Know Enum

      2. How to use EnumSet and EnumMap

      3. Enum Analysis

    An Example to Know Enum

      Firstly,we use the Enum to implements Operation.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package org.nsg.jdk.testEnum;
     
    /**
     * @Description  OperationTest.java
     *
     * @author BYSocket
     * @date 2015-1-8 6:05:59PM
     * @version 1.0
     */
    public class OperationTest
    {
        public static void main(String[] args)
        {
            double x = 2.0,y=4.0;
            for (Operation op : Operation.values())
                System.out.printf("%f %s %f = %f%n", x,op,y,op.apply(x, y));
        }
    }
     
    enum Operation
    {
        PLUS("+")
        {
            double apply(double x,double y){return x + y;}
        },
        MINUS("-")
        {
            double apply(double x,double y){return x - y;}
        },
        TIMES("*")
        {
            double apply(double x,double y){return x * y;}
        },
        DIVIDE("/")
        {
            double apply(double x,double y){return x / y;}
        };
         
        private final String symbol;
        Operation(String symbol){this.symbol = symbol;}
         
        @Override
        public String toString(){return symbol;}
         
        abstract double apply(double x,double y);
    }

      Run as Java application,we can see the Console.The result shows operations

    1
    2
    3
    4
    2.000000 + 4.000000 = 6.000000
    2.000000 - 4.000000 = -2.000000
    2.000000 * 4.000000 = 8.000000
    2.000000 / 4.000000 = 0.500000

    Q:‘The enum is just like class?’ 

    A:Yep,I think that Enum is a nice type.So let us know some methods by apis:

    1. Firstly,we can make an abstract method ‘apply()’ ,then set in the constant-specific class body. Its called constant-specific method implementation.

    2. We can make constructor with fields to make the enum has vales.(Like String or int …)

    3. toString() method : Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn’t necessary or desirable. An enum type should override this method when a more programmer-friendly string form exists.

    4. ‘vales()’ method :to get all enum objects. And ‘getValue()’ can get the enum object’ value.

    Note ‘Its easy to learn how to use.Then learn more and study in depth.’ And in real projects,We can use enums to replace Int or String Enum Pattern.And Enum is also a typesafe enum.

    How to use EnumSet and EnumMap

      Let us see another example to learn some Sets of Enum.So lets see it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    package org.nsg.jdk.testEnum;
     
    import java.util.EnumMap;
    import java.util.EnumSet;
    import java.util.Iterator;
    import java.util.Map.Entry;
     
    /**
     * @Description  WeekTest.java
     *
     * @author BYSocket
     * @date 2015-1-9 2:55:10PM
     * @version 1.0
     */
    public class WeekTest
    {
        public static void main(String[] args)
        {
            EnumSet<Week> weekSet = EnumSet.allOf(Week.class);
            System.out.println("EnumSet:");
            for (Week w : weekSet)
                System.out.println(w);
             
            EnumMap<Week, String> weekMap = new EnumMap<Week, String>(Week.class);
            weekMap.put(Week.MON, "星期一");
            weekMap.put(Week.TUE, "星期二");
            weekMap.put(Week.WED, "星期三");
             
            System.out.println("EnumMap:");
            for (Iterator<Entry<Week, String>> iterator = weekMap.entrySet().iterator(); iterator.hasNext();)
            {
                Entry<Week, String> weekEntry = iterator.next();
                System.out.println(weekEntry.getKey().name()+":"+weekEntry.getValue());
            }
        }
    }
     
    enum Week
    {
        MON("1"), TUE("2"), WED("3"), THU("4"), FRI("5"), SAT("6"),SUN("7");
     
        private final String symbol;
        Week(String symbol){this.symbol = symbol;}
         
        @Override
        public String toString(){return symbol;}
    }

    We can see in Console:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <font size="4" face="宋体">EnumSet:
    1
    2
    3
    4
    5
    6
    7
    EnumMap:
    MON:星期一
    TUE:星期二
    WED:星期三
    </font>

    Note: EnumSet or EnumMap is easy for we to use.And with them,we can use enums easily.

    Enum Analysis

      We use ‘javap -c -private xxx’to know the class:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    final class org.nsg.jdk.testEnum.Week extends java.lang.Enum<org.nsg.jdk.testEnu
    m.Week> {
      public static final org.nsg.jdk.testEnum.Week MON;
     
      public static final org.nsg.jdk.testEnum.Week TUE;
     
      public static final org.nsg.jdk.testEnum.Week WED;
     
      public static final org.nsg.jdk.testEnum.Week THU;
     
      public static final org.nsg.jdk.testEnum.Week FRI;
     
      public static final org.nsg.jdk.testEnum.Week SAT;
     
      public static final org.nsg.jdk.testEnum.Week SUN;
     
      private final java.lang.String symbol;
     
      private static final org.nsg.jdk.testEnum.Week[] $VALUES;
     
      public static org.nsg.jdk.testEnum.Week[] values();

      We can see ‘Enum is a class.just is a class.’but no extends.

    Writer:BYSocket(泥沙砖瓦浆木匠)

        微博:BYSocket

        豆瓣:BYSocket

    Reprint it anywhere u want.

  • 相关阅读:
    [CTF]Capture The Flag -- 夺旗赛
    [DesignPattern]Builder设计模式
    [Git]Git 常用的操作命令
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
    QT 实现 读取 增加 删除 实时操作xml
    QT5-图形视图框架
    C++之QT
    ,即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
    要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给
    给定N个非0的个位数字,用其中任意2个数字都可以组合成1个2位的数字。要求所有可能组合出来的2位数字的和。例如给定2、5、8,则可以组合出:25、28、52、58、82、85,它们的和为330。
  • 原文地址:https://www.cnblogs.com/Alandre/p/4213523.html
Copyright © 2011-2022 走看看