我的构造函数有效吗?

时间:2015-09-30 21:35:42

标签: java constructor this

对于编程作业,我被要求写

“a)没有值的通用构造函数。这个构造函数允许你使用所有字段设置为默认值的帐户。

b)一个带五个输入的构造函数。第一个是双倍,是帐户的利率,第二个是int,因为帐户的最低余额,第三个是双倍,是帐户的透支费用,第四个是双倍,是帐户的ATM费用,第五个是双倍的,是帐户的退回支票费用。“

我担心我的代码冗余且效率低下。我希望有人会告诉我什么是不必要的和/或我是否遗漏了什么重要的东西。如何在不手动初始化字段的情况下创建通用构造函数?另外,我有点担心我的第二个构造函数不会初始化除了作为参数列出的5个字段之外的任何字段。

public class BankAccount
{
  /*Balance of BankAccount*/
  private double balance = 0.00;

  /*Minimum balance allowed for BankAccount*/
  private int minimumBalance = 0;

  /*Interest rate of BankAccount*/
  private double interestRate = 0.00;

  /*Fee given everytime withdrawal is made via ATM*/
  private double ATMFee = 0.00;

  /*Amount deducted from balance if there is an overdraft*/
  private double overdraftFee = 0.00;

  /*Number of withdrawals allowed to be made before fee*/
  private int withdrawLimit = 0;

  /*Value to be deducted from balance if withdrawal limit is exceeded*/
  private double withdrawFee = 0.00;

  /*Keeps track of how many withdrawals owner has made*/
  private int withdrawCount = 0;

  /*Fee for bouncing a check*/
  private double bouncedCheckFee = 0.00;

  /*Stores interest earned*/
  private double interestEarned = 0.00;

  /*Whether or not overdraft fee has been charged*/
  private boolean overdraftFlag = false;

  /*Generic constructor takes no parameters and initializes field values to their default values*/
  public BankAccount()
  {
    this.balance = balance;
    this.minimumBalance = minimumBalance;
    this.interestRate = interestRate;
    this.ATMFee = ATMFee;
    this.overdraftFee = overdraftFee;
    this.withdrawLimit = withdrawLimit;
    this.withdrawFee = withdrawFee;
    this.withdrawCount = withdrawCount;
    this.bouncedCheckFee = bouncedCheckFee;
    this.interestEarned = interestEarned;
    this.overdraftFlag = overdraftFlag;
  }

  /*More specialized constructor takes 5 fields as parameters and initalizes them to their specified values*/
  public BankAccount(double interestRate, int minimumBalance, double overdraftFee, double ATMFee, double bouncedCheckFee)
  {
    this();
    this.interestRate = interestRate;
    this.minimumBalance = minimumBalance;
    this.overdraftFee = overdraftFee;
    this.ATMFee = ATMFee;
    this.bouncedCheckFee = bouncedCheckFee;
  }
}

编辑:好的,我根据大家的建议编辑了我的代码。

在这里。现在好吗?

public class BankAccount
{
  /*Balance of BankAccount*/
  private double balance = 0.00;

  /*Minimum balance allowed for BankAccount*/
  private int minimumBalance = 0;

  /*Interest rate of BankAccount*/
  private double interestRate = 0.00;

  /*Fee given everytime withdrawal is made via ATM*/
  private double ATMFee = 0.00;

  /*Amount deducted from balance if there is an overdraft*/
  private double overdraftFee = 0.00;

  /*Number of withdrawals allowed to be made before fee*/
  private int withdrawLimit = 0;

  /*Value to be deducted from balance if withdrawal limit is exceeded*/
  private double withdrawFee = 0.00;

  /*Keeps track of how many withdrawals owner has made*/
  private int withdrawCount = 0;

  /*Fee for bouncing a check*/
  private double bouncedCheckFee = 0.00;

  /*Stores interest earned*/
  private double interestEarned = 0.00;

  /*Whether or not overdraft fee has been charged*/
  private boolean overdraftFlag = false;

  /*Generic constructor takes no parameters and initializes field values to their default values*/
  public BankAccount()
  {
  }

  /*More specialized constructor takes 5 fields as parameters and initalizes them to their specified values*/
  public BankAccount(double interestRate, int minimumBalance, double overdraftFee, double ATMFee, double bouncedCheckFee)
  {
    this.interestRate = interestRate;
    this.minimumBalance = minimumBalance;
    this.overdraftFee = overdraftFee;
    this.ATMFee = ATMFee;
    this.bouncedCheckFee = bouncedCheckFee;
  }
}

3 个答案:

答案 0 :(得分:1)

在没有参数的构造函数中,您重新分配字段的值,这是超级的。如果您已经在声明中给了他们一个值,那么如果您不希望它具有不同的值,则不需要在构造函数中再次执行它。

答案 1 :(得分:1)

首先,您的默认BankAccount()构造函数应为空。它只是将字段分配给自己。

其次,请仅在构造函数中包含强制字段。请参阅我的回答here,并在创建后使用setter或使用Builder Pattern使用可选参数构建对象。

希望这有帮助!

答案 2 :(得分:0)

字段声明中已存在的默认构造函数重复字段初始化。你可以简单地清理它的身体。

代码的另一个限制是,当您必须创建对象时,必须指定构造函数中使用的所有字段。您可以使用fluent interfacebuilder pattern来避免这种情况,以使代码更具可读性。

现在我翻译我在代码中所说的内容。

BankAccountOptions类的代码:

public class BankAccountOptions {

    public static BankAccountOptions build() {
        return new BankAccountOptions();
    }

    public double aTMFee;

    public double balance;

    public double bouncedCheckFee;

    public double interestEarned;

    public float interestRate;

    public int minimumBalance;

    public double overdraftFee;

    public boolean overdraftFlag;

    public int withdrawCount;

    public double withdrawFee;

    public int withdrawLimit;

    public BankAccountOptions aTMFee(double value) {
        aTMFee = value;
        return this;
    }

    public BankAccountOptions balance(double value) {
        balance = value;
        return this;
    }

    public BankAccountOptions balance(int value) {
        minimumBalance = value;
        return this;
    }

    public BankAccountOptions bouncedCheckFee(double value) {
        bouncedCheckFee = value;
        return this;
    }

    public BankAccountOptions interestEarned(double value) {
        interestEarned = value;
        return this;
    }

    public BankAccountOptions interestRate(float value) {
        interestRate = value;
        return this;
    }

    public BankAccountOptions overdraftFee(double value) {
        overdraftFee = value;
        return this;
    }

    public BankAccountOptions overdraftFlag(boolean value) {
        overdraftFlag = value;
        return this;
    }

    public BankAccountOptions withdrawCount(int value) {
        withdrawCount = value;
        return this;
    }

    public BankAccountOptions withdrawFee(double value) {
        withdrawFee = value;
        return this;
    }

    public BankAccountOptions withdrawLimit(int value) {
        withdrawLimit = value;
        return this;
    }
}

班级BankAccount

public class BankAccount {

/* Fee given everytime withdrawal is made via ATM */
private double ATMFee;

/* Balance of BankAccount */
private double balance;

/* Fee for bouncing a check */
private double bouncedCheckFee;

/* Stores interest earned */
private double interestEarned;

/* Interest rate of BankAccount */
private double interestRate;

/* Minimum balance allowed for BankAccount */
private int minimumBalance;

/* Amount deducted from balance if there is an overdraft */
private double overdraftFee;

/* Whether or not overdraft fee has been charged */
private boolean overdraftFlag;

/* Keeps track of how many withdrawals owner has made */
private int withdrawCount;

/* Value to be deducted from balance if withdrawal limit is exceeded */
private double withdrawFee;

/* Number of withdrawals allowed to be made before fee */
private int withdrawLimit;

/* Generic constructor takes no parameters and initializes field values to their default values */
public BankAccount() {
   this(BankAccountOptions.build());
}

BankAccount(BankAccountOptions options)
}

/* More specialized constructor takes 5 fields as parameters and initalizes them to their specified values */
public BankAccount(BankAccountOptions options) {
    balance = options.balance;
    minimumBalance = options.minimumBalance;
    interestRate = options.interestRate;
    ATMFee = options.aTMFee;
    overdraftFee = options.overdraftFee;
    withdrawLimit = options.withdrawLimit;
    withdrawFee = options.withdrawFee;
    withdrawCount = options.withdrawCount;
    bouncedCheckFee = options.bouncedCheckFee;
    interestEarned = options.interestEarned;
    overdraftFlag = options.overdraftFlag;
}

}

现在假设您要使用默认值创建BankAccount对象。代码将是:

...
BankAccount account=new BankAccount();
...

如果您想创建一个帐户并仅修改两个属性(即minimumBalance和withdrawLimit),您可以写:

...
BankAccount account=new BankAccount(BankAccountOptions.build().minimumBalance(200).withdrawLimit(4));
...