3

对于分配,我们需要为从它创建的每个对象创建一个具有唯一 ID 的简化 BankAccount 类。这样做似乎最好的方法是属于类本身的静态 int,但尝试增加它并没有从 0 增加它的值。我在这里犯了什么错误?我认为这可能是微不足道的,但我似乎看不到它。

public class BankAccount {

// instance fields

/**
 * each BankAccount instance should have
 * a unique account number
 */

private int accountNumber;
private String accountHolder;
private double currentBalance;
private double overdraftLimit;

// static fields

private static int nextID;

// constructors

public void BankAccount(){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.overdraftLimit = 0;
}

public void BankAccount(String accountHolder, double overdraftLimit){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.accountHolder = accountHolder;
    this.overdraftLimit = overdraftLimit;
}
}

我定义了一个 main 方法,仅用于测试对象定义;这对班级本身来说是多余的。任何帮助将不胜感激!

编辑:供参考,不是其他链接问题的副本。这涉及初始化错误的构造函数,而不是 for 循环。

4

3 回答 3

8

不是构造函数

// this is method: 'bankAccountInstance.BankAccount()' 
public void BankAccount() 
{
    this.accountNumber = BankAccount.nextID++;
    //
}

// and this is method: 'bankAccountInstance.BankAccount("str", 5.1)' 
public void BankAccount(String accountHolder, double overdraftLimit)
{
    this.accountNumber = BankAccount.nextID++;
    //
}  

那是构造函数

// this is constructor 'BankAccount b = new BankAccount()'
public BankAccount() 
{
    this.accountNumber = BankAccount.nextID++;
    //
}

// and this is constructor 'BankAccount b = new BankAccount("Account", 1.0)'
public BankAccount(String accountHolder, double overdraftLimit)
{
    this.accountNumber = BankAccount.nextID++;
    //
}   

您可以在此处阅读有关构造函数的更多信息

于 2013-10-14T19:04:42.210 回答
4

问题是你认为你定义为构造函数的不是构造函数。它们只是方法,永远不会被调用。所以Java插入了一个默认构造函数,默认情况下它accountNumber总是将你初始化为0。

改变

public void BankAccount(){
public void BankAccount(String accountHolder, double overdraftLimit){

public BankAccount(){
public BankAccount(String accountHolder, double overdraftLimit){
于 2013-10-14T19:05:30.820 回答
1

一些问题:

  • 下面不是构造函数。

    public void BankAccount(){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.overdraftLimit = 0;
    

    }

    public void BankAccount(String accountHolder, double overdraftLimit){
    this.accountNumber = BankAccount.nextID++;
    this.currentBalance = 0;
    this.accountHolder = accountHolder;
    this.overdraftLimit = overdraftLimit;
    

    }

对于构造函数,请遵循以下几行

public BankAccount(){
        this.accountNumber = BankAccount.nextID++;
        this.currentBalance = 0;
        this.overdraftLimit = 0;
    }
  • nextID 应声明为 AtomicInteger:

        Integer currentInteger = atomicInteger.get();
        Integer nextInteger;
        do {
            currentInteger = atomicInteger.get();
            nextInteger = currentInteger + 1;
        } while (atomicInteger.compareAndSet(currentInteger, nextInteger));
    
  • 像下面这样初始化静态变量是很好的(不是强制性的)

    private static int nextID;
    
    static{
        nextID=0;
    }
    
  • 最好先递增而不是分配而不是反向(非强制性)

        this.accountNumber = ++BankAccount.nextID;
    
于 2013-10-14T19:08:05.510 回答