题目:
图片说明
图片说明

Account父类:

package shangguigu;

public class Account {
//    成员属性
    private int id;
    private double balance;
    private double annullInterestRate;

//    成员方法:构造器,get,set方法
    public Account(int id, double balance, double annullInterestRate) {
        super();
        this.id = id;
        this.balance = balance;
        this.annullInterestRate = annullInterestRate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnullInterestRate() {
        return annullInterestRate;
    }

    public void setAnnullInterestRate(double annullInterestRate) {
        this.annullInterestRate = annullInterestRate;
    }
// 成员方法:主要方法
//    返回月利率的方法 getMonthlyInterest()
    public double getMonthlyInterest() {
        return     annullInterestRate / 12;    
    }
//    取款方法 withdraw()
    public void withdraw(double amount) {
        if(balance >= amount) {
            balance -= amount;
        }
        else {
            System.out.println("余额不足!");
        }
    }
//    存款方法deposit()
    public void deposit(double amount) {
        balance += amount;
    }


}

CheckAccount子类:

package shangguigu;

public class CheckAccount extends Account {
    private double overdraftLimit;

    public CheckAccount(int id, double balance, double annullInterestRate, double overdraftLimit) {
        super(id, balance, annullInterestRate);
        this.overdraftLimit = overdraftLimit;
    }
    public double getOverdraftLimit() {
        return overdraftLimit;
    }
    public void setOverdraftLimit(double overdraftLimit) {
        this.overdraftLimit = overdraftLimit;
    }



    // 重写取款方法 withdraw()
    public void withdraw(double amount) {
        if (super.getBalance() >= amount) {
            super.setBalance(super.getBalance() - amount);
        } else {
            if (amount - super.getBalance() > overdraftLimit) {
                System.out.println("超过可透支额度!");
            } else {
                overdraftLimit = overdraftLimit - amount + super.getBalance();
                super.setBalance(0.0);

            }

        }
    }
}

测试类:

package shangguigu;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheckAccount a = new CheckAccount(1122, 20000, 0.045,5000);
        a.withdraw(5000);
        System.out.println("您的账户余额为:" + a.getBalance() + "\t您的可透支额度为:" + a.getOverdraftLimit());
        a.withdraw(18000);
        System.out.println("您的账户余额为:" + a.getBalance() + "\t您的可透支额度为:" + a.getOverdraftLimit());
        a.withdraw(3000);
        System.out.println("您的账户余额为:" + a.getBalance() + "\t您的可透支额度为:" + a.getOverdraftLimit());    
    }

}

编译结果:
图片说明