简体   繁体   English

声明后填充数组成员

[英]Populate array members after the declaration

My class has an array as one of it's members. 我的班级有一个数组作为它的成员之一。 Inside the constructor, I want to populate it's members to the following. 在构造函数内部,我想将其成员填充到以下内容中。 How can I do that, it gives me an error. 我该怎么办,这给我一个错误。

public class GetCurrentMonth {

    Calendar now;
    String[] monthNames;

    /**
     * 
     */
    public GetCurrentMonth() {
        now = Calendar.getInstance();
        monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    }

    public String getMonth()
    {
        return monthNames[(now.get(Calendar.MONTH) + 1)];
    }
}

I do not want to add these month names at the constructor as follows, since I want to follow good coding practice, and I was told that all initializations should be done inside the constructor. 我不想按如下方式在构造函数中添加这些月份的名称,因为我想遵循良好的编码习惯,并且被告知所有初始化都应在构造函数内部完成。

String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Firstly, not only is it not good practice to initialize in the constructor, it's actually bad practice, because: 首先,在构造函数中初始化不仅不是好的习惯,而且实际上是一种不好的习惯,因为:

  • you actually have to code a constructor (adding useless code) 您实际上必须编写一个构造函数(添加无用的代码)
  • fields should be static final if they're the same for every instance, like the months 如果每个实例的字段都相同,则字段应为static final
  • instance fields should final where possible and be initialised on declaration if there's only one way to initialize them 如果仅有一种初始化实例字段的方法,则实例字段应尽可能在final声明并初始化

Best practice, including adhering to naming standards, says your class should look like this: 包括遵循命名标准在内的最佳实践表明,您的课程应如下所示:

public class GetCurrentMonth {
    private static final String[] MONTH_NAMES = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    private final Calendar now = Calendar.getInstance();

    public String getMonth() {
        return MONTH_NAMES [(now.get(Calendar.MONTH) + 1)];
    }
}

您需要使用新的String []创建数组

monthNames = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

First, I agree with the answer from Bohemian♦, But I even would go further and recommend that you create an Enum that holds your month names. 首先,我同意Bohemian♦的回答,但是我什至会更进一步建议您创建一个包含月份名称的枚举。 Here is how I would do it: 这是我的方法:

public enum Month {
    JANUARY("January"), 
    FEBRUARY("February"), 
    MARCH("March"), 
    APRIL("April"), 
    MAY("May"),
    JUNE("June"), 
    JYLY("July"), 
    AUGUST("August"), 
    SEPTEMBER("September"),
    OCTOBER("October"), 
    NOVEMBER("November"), 
    DECEMBER("December");

    private String displayName;

    private Month(String displayName) {
        this.displayName = displayName;
    }

    @Override
    public String toString() {
        return displayName;
    }

    public static Month valueOfIgnoreCase(String value) {
        return valueOf(value.toUpperCase());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM