String作为参数

时间:2011-04-04 19:18:40

标签: java

//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the account.
//*******************************************************
import java.text.NumberFormat;

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");
  }

  //----------------------------------------------
  // 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()
  {
    NumberFormat fmt = NumberFormat.getCurrencyInstance();

   return (acctNum + "\t" + name + "\t" + fmt.format(balance));
    }

  //----------------------------------------------
  // Deducts $10 service fee
  //----------------------------------------------
  public double chargeFee()
  {
    balance=balance-10;
     return balance;
  }

  //----------------------------------------------
  // Changes the name on the account 
  //----------------------------------------------
  public void changeName(String newName)

  {
    name=String.toString(newName);
  }

}

我需要帮助完成此程序的最后一部分//更改帐户名称。我需要这样做,以便它将一个字符串(名称)作为参数并将其更改为一个新的字符串(newName),什么是正确的语法?我在书中找不到它。

2 个答案:

答案 0 :(得分:2)

name = newName;

工作得很好。字符串是不可变的,因此之后无法更改。

答案 1 :(得分:1)

它是:

public void changeName(String newName)
{
    name=newName;
}