5

我正在做一个学校作业,我应该使用断言来测试存款方法和构造函数的先决条件。我想出了方法,但我被困在如何添加到构造函数上。

这是我到目前为止所拥有的:

/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/
public class BankAccount
{  
   private double balance;

   /**
      Constructs a bank account with a zero balance.
   */
   public BankAccount()
   {   
      balance = 0;
   }

   /**
      Constructs a bank account with a given balance.
      @param initialBalance the initial balance
   */
   public BankAccount(double initialBalance)
   {   
      balance = initialBalance;
   }

   /**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(double amount)
   {  
      assert amount >=0;

       double newBalance = balance + amount;
      balance = newBalance;
   }

   /**
      Withdraws money from the bank account.
      @param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {   
      double newBalance = balance - amount;
      balance = newBalance;
   }

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {   
      return balance;
   }
}
4

3 回答 3

5
/**
   Constructs a bank account with a zero balance.
*/
public BankAccount()
{   
   this(0);
}

/**
   Constructs a bank account with a given balance.
   @param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{   
    assert initialBalance >= 0;
    balance = initialBalance;
}

创建任何非法BankAccount立即导致异常(如果启用了断言)。

断言几乎可以放在代码中的任何地方。它们用于调试目的,如果您禁用它们(例如在生产期间),它们将不起作用。

如果银行帐户的目的始终是积极的,您可能需要添加其他断言,例如:

/**
   Withdraws money from the bank account.
   @param amount the amount to withdraw
*/
public void withdraw(double amount)
{   
    assert amount <= this.balance;

    this.balance -= amount;
}

同样,断言仅用于调试,您不应该尝试捕获它们。任何断言异常都表示程序中的错误(或断言语句中的错误)。

因此,不应使用以下内容:

try
{
    BankAccount newbankaccount = new BankAccount(5);
    newbankaccount.withdraw(6.0);
}
catch (Exception e)
{
    // illegal withdrawal
}

相反,您应该检查先决条件。

BankAccount newbankaccount = new BankAccount(5);
if (newbankaccount.getBalance() < 6.0)
{
    // illegal withdrawal
}
else
    newbankaccount.withdraw(6.0);

只有在您的应用程序中存在逻辑错误时,才应触发断言异常。

于 2013-08-11T00:37:25.873 回答
4

何时使用断言我的 2 美分;

使用 Assertions 时,请不要将其与 Exception 的使用混淆。

  • 在检查私有/内部代码的前置条件、后置条件和不变量时使用断言

  • 使用断言向您自己或您的开发团队提供反馈

  • 在检查不太可能发生的事情时使用断言,否则意味着您的应用程序中存在严重缺陷。

  • 使用断言来陈述你知道是真实的事情。

  • 检查传递给公共或受保护方法和构造函数的参数时使用异常

请看这个:

    public class BankAccount
    {  
        private double balance;

        /**
           Constructs a bank account with a zero balance.
        */
        public BankAccount()
        {   
            balance = 0;

            // assert is NOT used to validate params of public methods
            // if ( !isValidBalance(amount) ) {
            //     throw new IllegalArgumentException("Amount should be greater than zero.");
           // }

            //check the class invariant
            assert hasValidBalance(): "Construction failed.";

        }        
           // Implements the class invariant.
           // Perform all checks on the state of the object.
           // One may assert that this method returns true at the end
           // of every public method.

          private boolean hasValidBalance(){
              return balance>0;
          }
    }

希望这可以帮助。

于 2013-08-11T01:02:07.083 回答
2

Bas 涵盖了关于断言的重要部分。基本上,将在生产代码中跳过,但可以在调试期间启用。

考虑到这一点,我会将您的银行帐户代码的行为扩展为实际使用常规 Java if 语句;因为存入负数(或提取负数)总是错误的。

于 2013-08-11T00:54:59.733 回答