超载方法

时间:2011-04-11 17:49:25

标签: java

  //*******************************************************
// FlexibleAccount.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************
import java.util.Random;
public class FlexibleAccount
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public FlexibleAccount(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  public FlexibleAccount(double initBal, String owner, double number)
  {
    Random generator = new Random();
     balance = initBal;
    name = owner;
     number=generator.nextDouble();
     this.acctNum= number;
    }

    public FlexibleAccount(String owner)
    {
     balance = 0;
     name=owner;
     Random generator = new Random();
     number=generator.nextDouble();
     this.acctNum= number;
     }
  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  public void withdraw(double amount,int fee)

  {
   if(balance>=amount)
    balance-= amount+fee;
    else
        System.out.println("Insufficient funds");
    }   

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return "Name: " + name + 
"\nAccount Number: " + acctNum +
"\nBalance: " + balance; 
  }
}

这就是我所拥有的,我试图将FlexibleAccount超载3次,如下所示

  1. public FlexibleAccount(double initBal,String owner,long number) - 按指定初始化余额,所有者和帐号
  2. public FlexibleAccount(double initBal,String owner) - 按指定初始化余额和所有者;随机生成帐号。
  3. public FlexibleAccount(String owner) - 按指定初始化所有者;将初始余额设置为0并随机生成帐号。
  4. 当我编译时我得到这些错误

     FlexibleAccount.java:31: possible loss of precision
        found   : double
        required: long
             this.acctNum= number;
                           ^
        FlexibleAccount.java:39: cannot find symbol
        symbol  : variable number
        location: class FlexibleAccount
             number=generator.nextDouble();
             ^
        FlexibleAccount.java:40: cannot find symbol
        symbol  : variable number
        location: class FlexibleAccount
             this.acctNum= number;
                       ^
    

    如何解决这个问题,这是正确的重载方法吗?

1 个答案:

答案 0 :(得分:2)

就像上一个问题一样 - 当你在number中写acctNum = number;时,没有这样的变量public FlexibleAccount(String owner)

public FlexibleAccount(String owner)
    {
     balance = 0;
     name=owner;
     Random generator = new Random();
     number=generator.nextDouble();        << number is not declared
     this.acctNum= number;
     }

修改 这是你的第二个构造函数:

 public FlexibleAccount(double initBal, String owner, double number)
  {
    Random generator = new Random();
     balance = initBal;
    name = owner;
     number=generator.nextDouble();
     this.acctNum= number;
    }

出于某种原因,你用3个参数声明它,尽管你写道你希望它只接收2个参数 -

  

public FlexibleAccount(double   initBal,String owner) - 初始化   指定的余额和所有者;   随机生成帐号。

我想你想要变量号......它应该是更像这样的东西:

 public FlexibleAccount(double initBal, String owner)
      {
        Random generator = new Random();
         balance = initBal;
        name = owner;
         this.acctNum= generator.nextLong();
        }

现在正如你在上一篇中说的那样。问题这是一个功课,所以我不会添加。阅读你到目前为止所得到的答案和评论并进行处理。