这个C#代码的java等价代码是什么?

时间:2012-03-30 11:23:26

标签: c# java

我不确定如何将这个C#代码实现到java中? Dev是具有此代码的类。

    public enum ConfigSetupByte0Bitmap
    {
        Config5VReg = 0x80,
        ConfigPMux  = 0x40, 
    }

   public void SetVReg(bool val)
    {
        //vReg = val;
        if (val)
        {
            configSetupByte0 |= (int)Dev.ConfigSetupByte0Bitmap.Config5VReg;
        }
        else
        {
            configSetupByte0 &= ~(int)Dev.ConfigSetupByte0Bitmap.Config5VReg;   
        }
    }

2 个答案:

答案 0 :(得分:0)

我不是C#专家,但我认为这是功能相同的:

public void SetVReg(bool val) {
    if (val) {
        configSetupByte0 |= 0x80;
    } else {
        configSetupByte0 &= ~0x80;   
    }
}

其余的只是糖。


  

但在SetVReg方法中,它表示无法将ConfigSetupByte0Bitmap.Config5VReg转换为int。

没错。在Java中,枚举是对象类型,不能转换为整数。如果你想要一个带有整数“值”的Java枚举,你需要做一些事情:

    public enum Foo {
        ONE(1), THREE(3);
        public final value;
        Foo(int value) {
            this.value = value;
        }
    }

    // ...
    System.out.println("THREE is " + THREE.value);

答案 1 :(得分:0)

public enum ConfigSetupByte0Bitmap
{
    Config5VReg(0x80),
    ConfigPMux(0x40); 

    public final int value;

    private ConfigSetupByte0Bitmap(final int value)
    {
        this.value = value;
    }
}

public void SetVReg(boolean val)
{
    //vReg = val;
    if (val)
    {
        configSetupByte0 |= ConfigSetupByte0Bitmap.Config5VReg.value;
    }
    else
    {
        configSetupByte0 &= ~ConfigSetupByte0Bitmap.Config5VReg.value;   
    }
}
相关问题