gpt4 book ai didi

java - 如何在 Java 中从没有所有值的枚举创建对象

转载 作者:行者123 更新时间:2023-12-02 07:39:58 27 4
gpt4 key购买 nike

我有一个名为 PaymentFrequency 的枚举:

public enum PaymentFrequency {

D("Daily"),
W("Weekly"),
M("Monthly"),
Y("Yearly");

private final String description;

PaymentFrequency(final String description) {
this.description = description;
}

public String getDescription() {
return description;
}
}

现在如果我在我的 bean 中这样做:

private PaymentFrequency[] paymentPeriods = PaymentFrequency.values();

public PaymentFrequency[] getPaymentPeriods() {
return paymentPeriods;
}

public void setPaymentPeriods(PaymentFrequency[] paymentPeriods) {
this.paymentPeriods = paymentPeriods;
}

我将从枚举中获取所有值。如何仅获取值 WM

最佳答案

嗯,你可以这样做

 private PaymentFrequency[] paymentPeriods = 
new PaymentFrequency[] {PaymentFrequency.W, PaymentFrequency.M};

关于java - 如何在 Java 中从没有所有值的枚举创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com