Java基础语法51-继承和super的练习

本文展示了Java中继承的概念,通过Account基类和CheckAccount子类的实现,探讨了如何使用super关键字以及透支功能的实现。在CheckAccount类中,当账户余额不足时,会检查是否超过可透支限额,并进行相应的操作。
摘要由CSDN通过智能技术生成

Java基础语法51-继承和super的练习

本实验来自于尚硅谷java基础班,如果侵权请告知。
在这里插入图片描述
在这里插入图片描述

package com.atguigu.exer1;

public class Account {
    private int id;//账号
    private double balance;//余额
    private double annualInterestRate;//年利率

    public Account(int id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    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 getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    //返回月利率
    public double getMonthlyInterest(){
        return annualInterestRate/12;
    }
    //取钱
    public void withdraw (double amount){
        if(balance >= amount){
            balance -= amount;
        }
    }
    //存钱
    public void deposit (double amount){
        if(amount > 0){
            balance += amount;
        }
    }
}
package com.atguigu.exer1;

public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account(1122, 20000, 0.045);
        account.withdraw(30000);
        System.out.println("您的账户余额为:" + account.getBalance());
        account.withdraw(2500);
        System.out.println("您的账户余额为:" + account.getBalance());
        account.deposit(3000);
        System.out.println("您的账户余额为:" + account.getBalance());

        System.out.println("月利率为:" + account.getMonthlyInterest()*100+'%');
    }
}
package com.atguigu.exer1;

public class CheckAccount extends Account {

    private double overdraft;//可透支限额

    public double getOverdraft() {
        return overdraft;
    }

    public void setOverdraft(double overdraft) {
        this.overdraft = overdraft;
    }

    public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
        super(id, balance, annualInterestRate);
        this.overdraft = overdraft;
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {//余额足够的情况
//          setBalance(getBalance() - amount);
            super.withdraw(amount);
        } else if (overdraft >= amount - getBalance()) {//透支额度+余额足够消费
            overdraft -= (amount - getBalance());
            setBalance(0);
        }else{
            System.out.println("超过可透支限额!");
        }
    }
}
package com.atguigu.exer1;

public class CheckAccountTest {
    public static void main(String[] args) {
        CheckAccount checkAccount =new CheckAccount(1122, 20000, 0.045, 5000);
        checkAccount.withdraw(5000);
        System.out.println("您的账户余额为:" + checkAccount.getBalance());
        System.out.println("您的透支额度为:"+checkAccount.getOverdraft());
        checkAccount.withdraw(18000);
        System.out.println("您的账户余额为:" + checkAccount.getBalance());
        System.out.println("您的透支额度为:"+checkAccount.getOverdraft());
        checkAccount.withdraw(3000);
        System.out.println("您的账户余额为:" + checkAccount.getBalance());
        System.out.println("您的透支额度为:"+checkAccount.getOverdraft());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarxistVive

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值