Java 构造函数帮助

标签 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; 
  }
}

请查看//consolidate 部分。我想做的是,将 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);
                                                                                           ^

最佳答案

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

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

我建议你改成这样:

newAccount.setAcctNumber(generator.nextInt());

关于Java 构造函数帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626762/

相关文章:

java - 构造函数调用中的另一个方法定义?

java - 基于两个 COLUMNS 的 SQL Distinct 以及这两列值的交换应被视为一行

java - 如何将我的 requestdispatcher 发送到两个不同的 jsp 页面路径?

java - 为什么我的 InputStream 在 Android 中无法工作?

c# - C# 中的构造函数与对象初始值设定项优先级

python - 一般如何将参数传递给 super().__init__()?

java - 在处理 RecyclerView 列表项时使用继承是一种反模式吗?

java - 为什么 Apache Beam 2.2 写入 GCS 失败?

c++使用构造函数作为成员函数(用于序列化)

wpf - 使用 WPF Prism/Mahapps HamburgerMenu 控件时调用参数化 View 模型构造函数