简体   繁体   English

此C#代码的Java等价代码是什么?

[英]What would be java equivalent code for this C# code?

I am not sure how to implement this C# code into java? 我不确定如何在Java中实现此C#代码? Dev is class that has this code. 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;   
        }
    }

I'm not a C# expert, but I think that this is functionality equivalent: 我不是C#专家,但是我认为这与功能等效:

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

The rest is just sugar. 剩下的只是糖。


But in SetVReg method, it says cannot cast ConfigSetupByte0Bitmap.Config5VReg to int. 但是在SetVReg方法中,它说不能将ConfigSetupByte0Bitmap.Config5VReg强制转换为int。

That's right. 那就对了。 In Java, enums are object types, and can't be cast to integers. 在Java中,枚举是对象类型,不能转换为整数。 If you want a Java enum with an integer "value" you need to do something line this: 如果要使用带有整数“值”的Java枚举,则需要执行以下操作:

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

    // ...
    System.out.println("THREE is " + THREE.value);
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;   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM