每次创建银行帐户时,帐户 ID 都应增加 1,但每次我尝试提取 Id 时,我只会得到帐户 ID 为 0,任何建议,因为我完全按照我学习的书中的方式进行操作而且它仍然没有更新。
帐户构造函数:
public class BankAccount {
public static int bankID = 0;
//constructor called by BankAccount michaelsBank = new BankAccount();
public BankAccount(){
balance = 0;
lastAssignedNumber++;
accountNumber = lastAssignedNumber;
}
//Constructs a bank account with an initial deposit, will be used if given a number for a parameter
public BankAccount(double initialBalance){
balance = initialBalance;
}
public void deposit(double amount){
balance = balance + amount;
}
public void withdraw(double amount){
balance = balance - amount;
}
public double getBalance(){
return balance;
}
public int getID(){
return accountNumber;
}
private double balance;
private int accountNumber;
private static int lastAssignedNumber;
}
银行账户主程序:
import java.text.*;
public class BankAccountTest {
public static void main (String args[]){
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMaximumFractionDigits(2); // Helps formatter format for final output
formatter.setMinimumFractionDigits(2);
ConsoleReader console = new ConsoleReader(System.in);
System.out.println("Hello, would you like to make a new bank account?");
String newA = console.readLine();
if(newA.equalsIgnoreCase("yes")){
System.out.println("How much would you like to deposit initially?");
double init = console.readDouble();
BankAccount account = new BankAccount(init);
System.out.println("Your account is created, what would you like to do? \n 1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
int option = console.readInt();
while(option == 1){
System.out.println(account.getBalance() + " Is your balance. \nWhat would you like to do next?");
System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
option = console.readInt();
}
while(option == 2){
System.out.println(account.getID() + " Is your account id.\nWhat would you like to do next?");
System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
option = console.readInt();
}
while(option == 3){
System.out.println("How much would you like to withdraw?");
double withdraw = console.readDouble();
account.withdraw(withdraw);
System.out.println("Your new balance is " + account.getBalance() + "\nWhat would you like to do next?");
System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
option = console.readInt();
}
while(option == 4){
System.out.println("How much would you like to deposit?");
double deposit = console.readDouble();
account.deposit(deposit);
System.out.println("Your new balance is " + account.getBalance() + "\n what would you like to do next?");
System.out.println("1: Get Balance \n 2: Get Account ID \n 3: Make a Withdrawl \n 4: Make a Deposit");
option = console.readInt();
}
}
}
}
您构建 BankAccount 对象的方式杂乱无章,其中是否分配 ID 取决于您使用的构造函数。如果您重写构造函数,使它们链接在一起,并使用一个负责所有核心职责的主构造函数和一个为主构造函数分配默认值和委托的辅助构造函数,那么初始化将具有一致的结果。
(术语是 Scala 的,构造函数链接在该语言中是强制性的。)
这里的主要构造函数是:
public BankAccount(double initialBalance){
balance = initialBalance;
lastAssignedNumber++;
accountNumber = lastAssignedNumber;
}
并添加一个辅助构造函数:
public BankAccount() {
this(0);
}
无论你调用哪个,你都会得到一个生成的 id。
(这类似于洛伦佐的回答 https://stackoverflow.com/a/23252741/217324,我赞成它清楚地描述了问题。不同之处在于他的链接是朝另一个方向进行的,因此默认值被分配然后被覆盖。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)