我有一个抽象类Account和一个子类SavingsAccount,但是当我创建SavingsAccount对象时,它没有分配数字,例如1001、1002、1003等。知道为什么吗?

import java.util.ArrayList;

public abstract class Account {
    private String accountType;
    private static double balance = 0;
    private static int accountId;
    private static int accountNumberCounter = 1000;
    private ArrayList<Account> accounts;

    public Account(String acType, int acNumber){
        accountType = acType;
        accountNumberCounter ++;
        accountId = accountNumberCounter;

    }

    public Account() {
        accountNumberCounter++;
        accountId = accountNumberCounter;
    }
    public void addAccounts(Account acc){
        accounts.add(acc);

    }
    public void deposit(double amount){
        balance += amount;
    }
    public abstract boolean withdraw(double value);


    public String getAccountInfo(){
        return "Account type: " + accountType + ", Account number: " + accountId;
    }

    public int getAccountNumber(){
        return accountId;
    }
    public String getAccount(){
        String accountInformation = "Account Number: : " + accountId + "\nAccount Type: " + accountType;
        return accountInformation;
    }
    public void closeCurrentAccount() {
        if (balance < 0) {
            System.out.println("Your balance: " + balance + "Close your debt");
        } else {
            System.out.println("Ending balance: " + balance);
        }
    }


}


这是SavingsAccount

public class SavingsAccount extends Account {
    private static double balance = 0;
    private static final double RATE = 1.0;
    private static  String accountType = "Savings Account";
    private static int accountId;


    public SavingsAccount(){
        super();
    }

    public double getBalance(){
        return balance;
    }

    public void deposit(double amount){
        balance = balance + amount;
    }

    public boolean withdraw(double amount){
        if (balance<= amount){
            System.out.println("You have only" + amount + "left on your account.");
        return false;
        }
        else{
            balance -= amount;
            System.out.println("You put:" + amount);
            return true;
        }
    }

    public static String getAccountType(){
        return accountType;
    }

    public static double getRate(){
        return RATE;
    }
    public static double calculateRate(){
        return balance += (balance * RATE) / 100;
    }
    public String getAccount(){
        String accountInformation = "Account Number: : " + accountId + "\nAccount Type: " + accountType +
            "\nBalance: " + balance + "\nRate: " + RATE;
        return accountInformation;
    }
}


也许不需要,但这也是Customer

import java.util.ArrayList;

public class Customer {
    private String name;
    private String surname;
    private String personalNumber;
    private ArrayList<Account> accounts;

    public Customer(String customerName, String customerSurname, String customerPersonalNumber)
    {
        name = customerName;
        surname = customerSurname;
        personalNumber = customerPersonalNumber;
        this.accounts = new ArrayList<Account>();
    }
    public Customer(){

    }
    public String getName(){
        return name;
    }
    public String getSurname(){
        return surname;
    }
    public String getPersonalNumber(){
        return personalNumber;
    }
    public void setName(String aName){
        name = aName;
    }
    public void setSurname(String aSurname){
        surname = aSurname;
    }

    public void setPersonalNumber(String aPersonalNumber){
        personalNumber = aPersonalNumber;
    }

    public void addAccounts(Account acc){
        accounts.add(acc);
    }

    public String getCustomerInfo(){
        return  name + " " +  surname + " " + personalNumber;
    }

    public int getFirstAccountNumber(){
        return accounts.get(0).getAccountNumber();

    }
    public int getLastAccountNumber(){
        return accounts.get(accounts.size()-1).getAccountNumber();
    }

    public ArrayList<Account> getAllAccounts(){
        return accounts;
    }
}


当我进行一些测试时,不会分配此唯一编号。

构造函数有问题吗?

最佳答案

您也将accountId标记为static,因此Account的每个实例都将获得相同的ID,即您“生成”的最新ID。

只需将accountId标记为普通实例变量即可(即删除static)。

附带说明一下,在accountId中重新声明SavingsAccount会破坏封装,坦率地说,这很奇怪。您从getAccountNumber()继承了Account。使用它而不是直接访问accountId。无论如何,您都将其视为只读变量。

关于java - 知道为什么我不能为我的SavingsAccount分配一个唯一的号码吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60264572/

10-12 20:56