3

ここに私の抽象クラスがあります:

public abstract class BankAccount{
  protected long balance;

  public BankAccount(long balance){ \\<--Abstract class constructor
    this.balance = balance;
  }

  ... more stuff
}

私は次のサブクラスを持っています (追加のサブクラス SavingsAccount もあり、どちらも独自の独立した残高を持っていますが、それは無関係です):

public class CurrentAccount extends BankAccount{
  private int PIN;
  private long overdraft = 0;
  private long balance;

  // Set balance and overdraft and the PIN
  public CurrentAccount(long balance, long overdraft, int PIN){
    super(balance);
    this.overdraft = overdraft;
    setPIN(PIN);
  }


  // Set balance and overdraft
  public CurrentAccount(long balance, long overdraft){
    super(balance);
    this.overdraft = overdraft;
  }

  // Set overdraft only
  public CurrentAccount(long overdraft){  \\<-- is it possible to have something like this?
    super(balance);
    this.overdraft = overdraft;
  }

  public void setPIN(int PIN){
    if(PIN >= 0000 && PIN <= 9999){ 
      this.PIN = PIN;
    }
  }

  ... more methods
}

上記からわかるように、当座貸越を設定するだけのコンストラクターが必要ですが、すべてのコンストラクターの開始時にまだ super を呼び出す必要があるため、現在の残高がどうであれ、渡すだけです。 ? または、CurrentAccount サブクラスにも残高変数が必要ですか?

Javaをコンパイルすると、次のようになります。

CurrentAccount.java:41: error: cannot reference balance before supertype constructor has been called
    super(balance);
          ^
1 error

どんな助けでも大歓迎です。

4

1 に答える 1