zoukankan      html  css  js  c++  java
  • java中通过位运算实现多个状态的判断

    通过 <<  |  & ~ 位运算,实现同时拥有多个状态

    通过 << 定义数据的状态

    public interface LogConstants {
        /**
         * 消耗标记
         */
        short COST_ASSET = 1 << 0;
        short COST_GOLD = 1 << 1;
        short COST_BINDGOLD = 1 << 2;
        short COST_SOPH = 1 << 3;
        short COST_STRSOUL = 1 << 4;
        short COST_REFSOUL = 1 << 5;
        short COST_SOULSTONE = 1 << 6;
        short COST_AAMHID = 1 << 7;
        short COST_REALM = 1 << 8;
    }

    通过 |= 加入多个状态

    short mark = 0;
            int costSoph = channel.getSoph();
            if (costSoph > 0) {
                mark |= LogConstants.COST_SOPH;
            }
    
            if (costGold > 0) {
                mark |= LogConstants.COST_GOLD;
            }

    通过 (m & LogConstants.COST_ASSET) > 0 判断是否拥有该状态

    通过 m = (short) (m & ~LogConstants.COST_ASSET);扣除该状态

    public void addCostLog(LogEvent event, Player player, short mark, int... args) {short m = mark;
            for (int i : args) {
                if ((m & LogConstants.COST_ASSET) > 0) {
                    m = (short) (m & ~LogConstants.COST_ASSET);
                    cost.setAsset(i);
                } else if ((m & LogConstants.COST_GOLD) > 0) {
                    m = (short) (m & ~LogConstants.COST_GOLD);
                    cost.setGold(i);
                } else if ((m & LogConstants.COST_BINDGOLD) > 0) {
                    m = (short) (m & ~LogConstants.COST_BINDGOLD);
                    cost.setBindGold(i);
                } else if ((m & LogConstants.COST_SOPH) > 0) {
                    m = (short) (m & ~LogConstants.COST_SOPH);
                    cost.setSoph(i);
                } else if ((m & LogConstants.COST_STRSOUL) > 0) {
                    m = (short) (m & ~LogConstants.COST_STRSOUL);
                    cost.setStrSoul(i);
                } else if ((m & LogConstants.COST_REFSOUL) > 0) {
                    m = (short) (m & ~LogConstants.COST_REFSOUL);
                    cost.setRefSoul(i);
                } else if ((m & LogConstants.COST_SOULSTONE) > 0) {
                    m = (short) (m & ~LogConstants.COST_SOULSTONE);
                    cost.setSoulStone(i);
                } else if ((m & LogConstants.COST_AAMHID) > 0) {
                    m = (short) (m & ~LogConstants.COST_AAMHID);
                    cost.setAamhid(i);
                } else if ((m & LogConstants.COST_REALM) > 0) {
                    m = (short) (m & ~LogConstants.COST_REALM);
                    cost.setRealm(i);
                }
            }
        }
  • 相关阅读:
    一些个人看到觉得还不错的资料,现在先把记得的保存下来,以后碰到会继续更新
    鼠标 mouseover和mouseout事件
    phpqrcode 生成二维码
    django url路径与模板中样式相对路径的问题
    js parseInt和map函数
    WebService 布置简单的计算器
    java ++的使用
    java 运算符
    Java的概念
    public 有跟没有的区别
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3948344.html
Copyright © 2011-2022 走看看