Java构造函数帮助

时间:2011-04-11 19:55:14

标签: java constructor

//*******************************************************
// Account.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 Account
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    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");
  }
//----------------
//Track how many accounts
//----------------
    private static int numAccounts=0;
    {
        numAccounts++;
        }
    public static int getNumAccounts()
    {
        return numAccounts;
        }

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

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }
// Get name of account
    public String getName()
    {
        return name;
    }
    //----------------------------------------------
  // Returns account number.
  //----------------------------------------------

  public long getAcctNumber()
  {
    return acctNum;
  }

//----------------
//Void and close the accounts
//----------------

    public void close()
{
    balance = 0;
    name += "CLOSE";
     numAccounts--;
     }

//----------------
//Consolidating accounts
//----------------
    public static Account consolidate(Account acct1,Account acct2)
    { Account newAccount=null;
        if((acct1.getName()).equals(acct2.getName()))
        if(acct1.getAcctNumber()!=acct2.getAcctNumber())
            {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);

                        Random generator = new Random();
            acctNum= generator.nextInt();
                acct1.close();
                acct2.close();
     }
     else
     System.out.println("Not allow,same account number");
     else
     System.out.println("Can't use other people account");
     return newAccount;
    }


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

请查看//合并部分。我要做的是,将acct1和acct2整合到一个新帐户中,限制为acct1和acct2必须具有相同的名称,acct1和acct2帐号必须彼此不同,如果是遇到了,使用两个旧帐户创建了一个具有新余额的新帐户,保持相同名称并随机生成一个新帐号。我的代码中是否缺少某些内容?它不会编译。这些是我得到的错误


    Account.java:95: ')' expected
                {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);
                                                                                     ^
    Account.java:95: illegal start of expression
                {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner);
                                                                                           ^

2 个答案:

答案 0 :(得分:5)

String owner应该是acct1.getName()或任何函数检索名称。

此外,行acctNum = generator.nextInt();将失败,因为在该上下文中未定义acctNum。此外,您不会将newAccount的帐号设置为此acctNum变量。

我建议你改成它:

newAccount.setAcctNumber(generator.nextInt());

答案 1 :(得分:0)

行的编译错误

newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner)

是因为String owner是一个应该在方法签名中使用的声明,如上所述。但是,当您实际调用构造函数时,需要发送String参数。

编译器抱怨,因为您在此行实际执行的操作是声明名为String的{​​{1}}变量。 Java不允许在方法调用中使用它。

Finbarr是对的;使用该方法来获取帐户的名称。