0

I'm working on a classic homework program and cannot for the life of me figure out why my static variable in the superclass reacts the way it does..

The program is a bankaccount where I have created a superclass, Account, and two subclasses, CreditAccount and SavingsAccount.

public abstract class Account {

  private double balance;
  private int accountId;
  **private static int lastAssignedNumber = 1000;**  <--- the static int
  private String accountType;

  public Account (double q_balance, String q_accountType)
  { 
    balance = q_balance;
    accountType = q_accountType;
    **accountId = ++lastAssignedNumber; <------ counter for new accountId**
  }

)

public class CreditAccount extends Account {

  public CreditAccount(double balance) 
  {
    super(balance, "Creditaccount");
  }

}

public class SavingsAccount extends Account {

  public SavingsAccount(double balance) 
  {
    super(balance, "Savingsaccount");
  }

}

Previously, without subclasses when Account was the only object, the counter worked beautifully. But now when I create some new objects of savingsaccount and creditaccounts the program acts really weird and returns accountnumbers as follows:

           new SavingsAccount(0);   // **1001**
    new CreditAccount(0);   // **1001**
    new CreditAccount(0);   // **1002**
    new SavingsAccount(0); // **1003**
    new CreditAccount(0);   // **1002**
    new CreditAccount(0);   // **1004**
    new SavingsAccount(0); // **1005**

What in gods name is happening?! What am I missing? Shouldn't the two subclasses provoke the same static variable 'lastAssignedNumber' and add to it accordingly??

Kindest regards // Gewra

4

2 回答 2

0

坦率地说,多线程和单线程的概念对我来说是全新的,但我尝试过制作 AtomicInteger 和 volatile 变量,结果完全相同。我想这是我的程序结构根本错误。

该构造是一个 BankLogic 类,它包含一个客户对象的 ArrayList。Customer-objects 拥有一个 Account-objects 的 ArrayList。我把 AtomicInteger 对象放在哪里并不重要,即使我把它放在 BankLogic 类中并将它传递给构造函数,它仍然会得到相同的结果。

猜猜我应该把accounts-ArrayList放在BankLogic类中,然后运行一个比较个人ID的方法(向账户类添加一个persId变量)?它当然感觉不像是一个优雅的解决方案,但我看不到其他方法。

感谢所有的答案!

于 2013-02-26T12:53:34.520 回答
0

鉴于您在单线程模型中创建帐户,您的代码没有任何问题。您的以下代码运行良好:

abstract class Account 
{

    private double balance;
    private int accountId;
    private static int lastAssignedNumber = 1000;
    private String accountType;

    public Account (double q_balance, String q_accountType)
    { 
        balance = q_balance;
        accountType = q_accountType;
        accountId = ++lastAssignedNumber;
    }
    public int getAccountID()
    {
        return accountId;
    }

}
class CreditAccount extends Account 
{
    public CreditAccount(double balance) 
    {
        super(balance, "Creditaccount");
    }

}
class SavingsAccount extends Account 
{
    public SavingsAccount(double balance) 
    {
        super(balance, "Savingsaccount");
    }
}
public class AccountLedger
{
    public static void main(String st[])
    {
        Account ac[] = new Account[7];
        ac[0] = new SavingsAccount(0); //1001  
        ac[1] = new CreditAccount(0);  //1002  
        ac[2] = new CreditAccount(0);  //1003  
        ac[3] = new SavingsAccount(0); //1004  
        ac[4] = new CreditAccount(0);  //1005  
        ac[5] = new CreditAccount(0);  //1006  
        ac[6] = new SavingsAccount(0); //1007  
        for (int i = 0 ; i < ac.length ; i++)
        {
            System.out.println(ac[i].getAccountID());
        }
    }
}
于 2013-02-25T21:15:15.787 回答