2012-03-30 101 views
0

我不知道如何實現這個C#代碼到Java? Dev是有這個代碼的類。這個C#代碼的java等效代碼是什麼?

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; 
     } 
    } 
+0

我創建枚舉符合上述枚舉與構造。但是在SetVReg方法中,它說不能將ConfigSetupByte0Bitmap.Config5VReg轉換爲int。 – Milan 2012-03-30 11:29:29

回答

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; 
    } 
} 
+0

謝謝,你能告訴我可能的方式來隱藏這個if(Enum.IsDefined(typeof(AdcChannels),channel)){....} – Milan 2012-03-30 12:46:52

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);