将值设置为枚举 - Java

发布于 2024-12-01 04:09:25 字数 237 浏览 0 评论 0原文

我正在尝试在我的 java 应用程序中将值设置为枚举......但我不能这样做。

我做错了吗???

public enum RPCPacketDataType {
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5)
};

它给了我这个错误:构造函数 RPCPacket.RPCPacketDataType(int) 未定义。

I'm trying to set values to enum in my java application....but I can't do that.

Am I doing it wrong???

public enum RPCPacketDataType {
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5)
};

It's giving me this error : The constructor RPCPacket.RPCPacketDataType(int) is undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月如刀 2024-12-08 04:09:25
public enum RPCPacketDataType
{
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5);

    RPCPacketDataType (int i)
    {
        this.type = i;
    }

    private int type;

    public int getNumericType()
    {
        return type;
    }
}

您还可以像在“普通”类中一样在枚举上定义方法。

 System.out.println(RPCPacketDataType.PT_JSON.getNumericType() // => 4
public enum RPCPacketDataType
{
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5);

    RPCPacketDataType (int i)
    {
        this.type = i;
    }

    private int type;

    public int getNumericType()
    {
        return type;
    }
}

You can also define methods on your enum as you would in a "normal" class.

 System.out.println(RPCPacketDataType.PT_JSON.getNumericType() // => 4
触ぅ动初心 2024-12-08 04:09:25

您应该创建一个接受 int 参数的构造函数。还要添加一个 int 字段来保存传递的值。

public enum RPCPacketDataType {
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5);

    private int mValue;

    RPCPacketDataType(int value) {
        mValue = value;
    }
}

You should create a Contructor which accepts an int parameter. Also add an int field which will hold the passed value.

public enum RPCPacketDataType {
    PT_UNKNOWN(2),
    PT_JSON(4),
    PT_BINARY(5);

    private int mValue;

    RPCPacketDataType(int value) {
        mValue = value;
    }
}
素衣风尘叹 2024-12-08 04:09:25
public enum RPCPacketDataType { 

  PT_UNKNOWN(2), 
  PT_JSON(4),
  PT_BINARY(5); 

  private int type;

  RPCPacketDataType(int type) { 
    this.type = type; 
  }

  public int getNumericType() {
    return type;
  }

  public void setNumericType(int type) {
    this.type = type;
  }

  public static void main(String[] args) {
    RPCPacketDataType.PT_UNKNOWN.setNumericType(0);
    System.out.println("Type: "+RPCPacketDataType.PT_UNKNOWN.getNumericType()); 
    // Type: 0
  }

}

正如 #emboss 和 #Michael 所说的正确,你可以使用接受 ant int 的构造函数

public enum RPCPacketDataType { 

  PT_UNKNOWN(2), 
  PT_JSON(4),
  PT_BINARY(5); 

  private int type;

  RPCPacketDataType(int type) { 
    this.type = type; 
  }

  public int getNumericType() {
    return type;
  }

  public void setNumericType(int type) {
    this.type = type;
  }

  public static void main(String[] args) {
    RPCPacketDataType.PT_UNKNOWN.setNumericType(0);
    System.out.println("Type: "+RPCPacketDataType.PT_UNKNOWN.getNumericType()); 
    // Type: 0
  }

}

As both #emboss and #Michael said correctly you can use a Contructor which accepts ant int

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文