如何在没有Java中的所有值的情况下从枚举创建对象

时间:2012-07-31 07:46:32

标签: java enums

我有一个名为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;
}

我将从枚举中获取所有值。如何只获取值 W M

3 个答案:

答案 0 :(得分:1)

您可以添加数组

static final PaymentFrequency[] WM = { W, M };

答案 1 :(得分:1)

嗯,你可以做到

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

答案 2 :(得分:0)

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

我不确定这是不是你的问题......