美文网首页Java 杂谈程序员之家Java
让你的系统更安全--用枚举enum替代int常量

让你的系统更安全--用枚举enum替代int常量

作者: java成功之路 | 来源:发表于2019-05-09 21:51 被阅读4次

做应用系统时,我们往往假设用户是小白,那么为了保证系统的正常,我们往往会对用户的参数做限制,并且前后端都要对用户的参数做验证。那我们在设计的时候是否可以提前预防这种问题呢?其中的一种方式就是:用枚举enum替代int常量。

枚举的好处:

做应用系统时,我们往往假设用户是小白,那么为了保证系统的正常,我们往往会对用户的参数做限制,并且前后端都要对用户的参数做验证。那我们在设计的时候是否可以提前预防这种问题呢?其中的一种方式就是:用枚举enum替代int常量。

枚举的好处:

1. 类型安全性

2.使用方便性

示例1

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class EnumDemo {     
    enum Color{     
        RED(3),BLUE(5),BLACK(8),YELLOW(13),GREEN(28);          
        private int colorValue;     
        private Color(int rv){     
         this.colorValue=rv;       
        }   
        private int getColorValue(){
            return colorValue;
        }   

        private int value(){
            return ordinal()+1;
        }
}          
    public static void main(String[] args) { 
        for(Color s : Color.values()) {     
            //enum的values()返回一个数组,这里就是Seasons[]     
            System.out.println(s.value()+":"+s.name()+"="+s.getColorValue());     
        }     
    }     
} 
</pre>

output:

1:RED=3

2:BLUE=5

3:BLACK=8

4:YELLOW=13

5:GREEN=28

其中

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">    /**
     * Returns the ordinal of this enumeration constant (its position
     * in its enum declaration, where the initial constant is assigned
     * an ordinal of zero).
     *
     * Most programmers will have no use for this method.  It is
     * designed for use by sophisticated enum-based data structures, such
     * as {@link java.util.EnumSet} and {@link java.util.EnumMap}.
     *
     * @return the ordinal of this enumeration constant
     */
    public final int ordinal() {
        return ordinal;
    }
</pre>

示例2:

EnumMap是专门为枚举类型量身定做的Map实现。虽然使用其它的Map实现(如HashMap)也能完成枚举类型实例到值得映射,但是使用EnumMap会更加高效:它只能接收同一枚举类型的实例作为键值,并且由于枚举类型实例的数量相对固定并且有限,所以EnumMap使用数组来存放与枚举类型对应的值。这使得EnumMap的效率非常高。

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.*;
public enum Phase {
    SOLID, LIQUID, GAS;
    public enum Transition {
        MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID), BOIL(LIQUID, GAS), CONDENSE(
                GAS, LIQUID), SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);
        private final Phase src;
        private final Phase dst;
        Transition(Phase src, Phase dst) {
            this.src = src;
            this.dst = dst;
        }
        private static final Map<Phase, Map<Phase, Transition>> m = new EnumMap<Phase, Map<Phase, Transition>>(
                Phase.class);
        static {
            for (Phase p : Phase.values())
                m.put(p, new EnumMap<Phase, Transition>(Phase.class));
            for (Transition trans : Transition.values())
                m.get(trans.src).put(trans.dst, trans);
        }
        public static Transition from(Phase src, Phase dst) {
            return m.get(src).get(dst);
        }
    }
    public static void main(String[] args) {
        for (Phase src : Phase.values())
            for (Phase dst : Phase.values())
                if (src != dst)
                    System.out.printf("%s to %s : %s %n", src, dst,
                            Transition.from(src, dst));
    }
}
</pre>

相关文章

  • 让你的系统更安全--用枚举enum替代int常量

    做应用系统时,我们往往假设用户是小白,那么为了保证系统的正常,我们往往会对用户的参数做限制,并且前后端都要对用户的...

  • EffectiveJava第6章-枚举和注解

    第30条:用enum代替int常量 (1)int枚举模式 比较脆弱,如果与枚举常量关联的int发生了变化,客户端需...

  • EffectiveJava-5-枚举和注解

    用enum代替int常量 1. int枚举: 引入枚举前,一般是声明一组具名的int常量,每个常量代表一个类型成员...

  • 枚举和注解

    30,用enum代替int常量 枚举类型是指由一组固定的常量组成合法值的类型。 与int常量相比,枚举的优势是不言...

  • effective java 第三周

    第6章 枚举和注解 第30条:用 enum 代替 int 常量 在没有 enum 之前表示枚举类型的常用模式时声...

  • Java核心类-枚举类

    Java 进阶——枚举enum使用小结及使用枚举替代你的常量类 通过enum定义的枚举类,和其他的class没有任...

  • Effective Java 2

    五、枚举和注解 30、用enum代替int常量 132,135 31、用实例域代替序数 不要依赖于枚举的顺序编程 ...

  • Effective java笔记(五),枚举和注解

    30、用enum代替int常量 枚举类型是指由一组固定的常量组成合法值的类型。在java没有引入枚举类型前,表示枚...

  • Effective Java(第3版)第6章总结

    第34条:用 enum 代替 int 常量 用枚举的情况一般都是某个类别的常量。比如星期、月份、颜色等等。 第35...

  • Android基础进阶之EffectiveJava翻译系列(第五

    Java1.5中提供的两种新类型 Item30: 用枚举替代int型常量 枚举:一系列常量类型的集合没有枚举前大量...

网友评论

    本文标题:让你的系统更安全--用枚举enum替代int常量

    本文链接:https://www.haomeiwen.com/subject/rvfnoqtx.html