java 枚举 创建_从字符串创建Java枚举

2022-09-19 14:29:13

本问题已经有最佳答案,请猛点这里访问。

我有枚举类

public enum PaymentType {

/**

* This notify type we receive when user make first subscription payment.

*/

SUBSCRIPTION_NEW("subscr_signup"),

/**

* This notify type we receive when user make subscription payment for next

* month.

*/

SUBSCRIPTION_PAYMENT("subscr_payment"),

/**

* This notify type we receive when user cancel subscription from his paypal

* personal account.

*/

SUBSCRIPTION_CANCEL("subscr_cancel"),

/**

* In this case the user cannot change the amount or length of the

* subscription, they can however change the funding source or address

* associated with their account. Those actions will generate the

* subscr_modify IPN that we are receiving.

*/

SUBSCRIPTION_MODIFY("subscr_modify"),

/**

* Means that the subscription has expired, either because the subscriber

* cancelled it or it has a fixed term (implying a fixed number of payments)

* and it has now expired with no further payments being due. It is sent at

* the end of the term, instead of any payment that would otherwise have

* been due on that date.

*/

SUBSCRIPTION_EXPIRED("subscr_eot"),

/** User have no money on card, CVV error, another negative errors. */

SUBSCRIPTION_FAILED("subscr_failed");

private String type;

PaymentType(String type) {

this.type = type;

}

String getType() {

return type;

}

}

尝试创建枚举时:

PaymentType type = PaymentType.valueOf("subscr_signup");

Java向我抛出错误:

IllegalArgumentException occured : No enum constant models.PaymentType.subscr_signup

我怎么能解决这个问题?

我认为您必须在枚举中编写一个比较方法

stackoverflow.com/a/2965252/5188230

创建额外的静态方法,该方法将搜索枚举的值

@不是复制品。如果字符串不是同一枚举值,则提供的示例不是解决方案。

@Gregkopff提供了正确的答案。

@ssh你读过所提供问题的第二个答案了吗?

@ssh您可能希望滚动一点,而不仅仅是为了获得已接受的答案。

@谢谢你。那是我要找的!

添加并使用此方法

public static PaymentType parse(String type) {

for (PaymentType paymentType : PaymentType.values()) {

if (paymentType.getType().equals(type)) {

return paymentType;

}

}

return null; //or you can throw exception

}

对。它是正确的。

枚举尚未准备好对此使用方法。您要么需要使用准确的字段名:

PaymentType.valueOf("SUBSCRIPTION_MODIFY");

或者编写自己的方法,例如:

public static PaymentType fromString(String string) {

for (PaymentType pt : values()) {

if (pt.getType().equals(string)) {

return pt;

}

}

throw new NoSuchElementException("Element with string" + string +" has not been found");

}

所以这个代码:

public static void main(String[] args) {

System.out.println(PaymentType.fromString("subscr_modify"));

}

印刷品:

SUBSCRIPTION_MODIFY

subscr_signup

是枚举变量值,而不是枚举值。因此,如果要根据枚举的值名称获取枚举,则必须这样做:

PaymentType type = PaymentType.valueOf("SUBSCRIPTION_NEW");

Enum类.valueOf()方法将传递给它的字符串与枚举实例的name属性(实例名称的实际值)进行比较,因此在您的示例中,这将是"SUBSCRIPTION_NEW"而不是"subscr_signup"。

您需要编写一个静态方法,该方法为给定值返回正确的枚举实例。例如:

public static PaymentType byTypeString(String type) {

for (PaymentType instance : values()) {

if (instance.type.equals(type)) {

return instance;

}

}

throw new IllegalArgumentException("No PaymentType enum instance for type:" + type);

}

我不能用它。

为什么不?如果不能修改paymentType枚举,只需将其作为静态方法添加到其他地方,使用getType()访问器而不是直接使用type。

valueOf返回其标识符与您传入的字符串匹配的枚举项。例如:

PaymentType t = PaymentType.valueOf("SUBSCRIPTION_NEW");

如果要获取其type字段与给定字符串匹配的枚举项,请在PaymentType上编写一个静态方法,该方法通过PaymentType.values()循环并返回匹配项。

我不能用它,因为我不能改变常量。

当然你可以写一个方法。

  • 作者:Stakey
  • 原文链接:https://blog.csdn.net/weixin_35059431/article/details/114597967
    更新时间:2022-09-19 14:29:13