java - Java 中枚举构造函数的问题

标签 java enums

我不确定枚举 Family 是否处于正确的位置,但是无论它在我的主类内部还是外部,我都会遇到相同的错误。这些枚举; WILMA、FRED、BETTY 和 BARNEY 将提供 数据来计算他们的退休储蓄。

public class Assignment5
{

    enum Family
    {
        WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35),
        FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty 
        BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25), 
        BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35),
    }

        //The properties’ “getters”/accessors should go here.

        //instance fields
        private final String firstName; //first names
        private final String lastName; //last names
        private final int annualDeposit; //annual deposit
        private final double annualRate; //annual rate
        private final int yearsDeposit; //number of years depositing
        private final int yearsCalc; //number of years to compound

        //enum family constructor
        Assignment5(String firstName, String lastName, int annualDeposit, double annualRate, int   yearsDeposit, int yearsCalc)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.annualDeposit = annualDeposit;
            this.annualRate = annualRate;
            this.yearsDeposit = yearsDeposit;
            this.yearsCalc = yearsCalc;
        }

        //getter for firstName
        public String getFirstName()
        {
            return firstName;
        }

        //getter for lastName
        public String getLastName()
        {
            return lastName;
        }

        //getter for annual deposit
        public int getAnnualDeposit()
        {
            return annualDeposit;
        }

        //getter for annual rate
        public double getAnnualRate()
        {
            return annualRate;
        }

        //getter for years deposit
        public int getYearsDeposit()
        {
            return yearsDeposit;
        }

        //getter for years calc
        public int getYearsCalc()
        {
            return yearsCalc;
        }

        //main method
        //formulas for calculating retirement account and printing results will go here
        public static void main(String[] args)
        {

        }

}

最佳答案

要创建带有参数的枚举,您应该使用合适的构造函数。根据您的情况,将枚举更改为:

enum Family
{
    WILMA("Wilma", "Flintstone", 5000, 0.05, 10, 35),
    FRED("Fred", "Flintstone", 15000, 0.075, 7, 30), //fred might swap with betty 
    BETTY("Betty", "Rubble", 7500, 0.0375, 10, 25), 
    BARNEY("Barney", "Rubble", 5000, 0.09, 10, 35);
    //instance fields
    private final String firstName; //first names
    private final String lastName; //last names
    private final int annualDeposit; //annual deposit
    private final double annualRate; //annual rate
    private final int yearsDeposit; //number of years depositing
    private final int yearsCalc; //number of years to compound

    Family(String firstName, String lastName, int annualDeposit, double annualRate, int   yearsDeposit, int yearsCalc)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.annualDeposit = annualDeposit;
        this.annualRate = annualRate;
        this.yearsDeposit = yearsDeposit;
        this.yearsCalc = yearsCalc;
    }
}

关于java - Java 中枚举构造函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26615579/

相关文章:

java - 寻找带有抓手的可调整大小的 SWT (JFace) 文本区域组件

java - 打开 Android 应用程序时出错

c# - 如何在包含一个变量的类中使用多个属性?

java - 在heroku上运行spring boot应用程序时出现"Unable to read TLD from JAR file"

java - Spring Boot PagingAndSortingRepository搜索: Combine multiple params for complex search

java - SubGit Java 堆空间

c# - 类型转换类型的扩展方法

enums - 匹配包含附件数据的枚举的所有变体

java - 创建一个 Spring 枚举 bean 并传递方法调用的值

ios - Swift:枚举 UIModalTransitionStyle 的扩展