スーパークラスに送信する前に、サブクラスで受け取った可変量の値をチェックする必要があります。 プライベートスーパークラス変数に送信する前にサブクラスの値を確認してください
この
は//superclass
public class Account
{
private double balance;
public Account(double amount)
{
balance = amount;
}
public Account()
{
this(0.0);
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
public double getBalance()
{
return balance;
}
}
これは私が私のサブクラスのために持っているもので、私は私のスーパーのために持っているもので、私は私が何とかチェックするために、「この(金額)」を使用することになっています考えて
public class SafeAccount extends Account
{
public SafeAccount(double amount)
{
// something to check if amount is positive before sending it to
super
// if it's not positive, use no argument constructor to set balance ==
0.0
}
}
しかし、私はそれがどのように機能するか正確にはわかりません。
これは[LSP](https://en.m.wikipedia.org/wiki/Liskov_substitution_principle#Principle)に違反します: "*サブタイプで前提条件を強化することはできません*" - あなたのデザインを再考したいかもしれません。 –