public class BankTest {
public static void main(String[] args) {
CheckingAccount c = new CheckingAccount(300);
c.deposit(100);
c.withdraw(20);
}
}
class Bank {
private double balance;
public void Bank(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
while (balance < amount) {
System.out.println("余额不足,请重新输入金额");
break;
}
if (balance > amount) {
balance -= amount;
}
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
class CheckingAccount extends Bank {
public CheckingAccount(double balance) {
super(balance);
}
@Override
public void deposit(double amount) {
super.deposit(amount - 1);
}
@Override
public void withdraw(double amount) {
super.withdraw(amount + 1);
}
}
输出结果: 399.0 378.0
|