MollieZhang 2022-05-29 15:57 采纳率: 90%
浏览 26
已结题

java类的继承问题

java类的继承问题,如下代码,为什么我用子类的构造器创建子类对象用父类的构造器创建父类对象,子类获得到的父类的余额属性是0呢?

package B_five.A;

/**
 * @author MollieZhang
 * @create 2022-05-29 14:37
 */
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 Account() {
    }

    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;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", balance=" + balance +
                ", annualInterestRate=" + annualInterestRate +
                '}';
    }
    public boolean withdraw(double amount){
        if (amount >= 0.0 && amount <= balance){
            balance-=amount;
            System.out.println("成功取出"+amount+"CNY。"+"您当前的余额为"+balance+"CNY");
            return true;
        }else{
            System.out.println("输入数值不合法!");
            return false;
        }
    }

    public boolean deposit(double amount){
        if (amount >=0.0){
            balance+=amount;
            System.out.println("成功存入"+amount+"CNY。"+"您当前的余额为"+balance+"CNY");
            return true;
        }else{
            System.out.println("输入数值不合法!");
            return false;
        }
    }
}

package B_five.A;

/**
 * @author MollieZhang
 * @create 2022-05-29 14:41
 */
public class CheckAccount extends Account{
    private double overdraft;

    public CheckAccount(double overdraft) {
        this.overdraft = 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 String toString() {
        return super.toString()+"CheckAccount{" +
                "overdraft=" + overdraft +
                '}';
    }

    @Override
    public boolean withdraw(double amount) {
        if (amount >= 0 && amount <= super.getBalance()){
            super.setBalance(super.getBalance()-amount);
            System.out.println("成功取钱 "+amount+"CNY。您当前的余额为 "+super.getBalance()+"CNY。"+
                    "您当前的可透支余额为 "+overdraft+" CNY。");
            return true;
        }else if(amount > super.getBalance()){
            if ((amount - super.getBalance() <= overdraft)){
                overdraft-=amount - super.getBalance();
                super.setBalance(0);
                System.out.println("成功取钱 "+amount+"CNY。您当前的余额为 "+super.getBalance()+"CNY。"+
                        "您当前的可透支余额为 "+overdraft+" CNY。");
                return true;
            }else{
                System.out.println("您所输入的钱数超过可透支额的限额!");
                return false;
            }
        }
        return false;
    }
}


package B_five.A;

/**
 * @author MollieZhang
 * @create 2022-05-29 15:02
 */
public class Test {
    public static void main(String[] args) {

        CheckAccount checkAccount = new CheckAccount(1122,20000,0.045,5000);
        checkAccount.withdraw(5000);
        checkAccount.withdraw(18000);
        checkAccount.withdraw(3000);

        System.out.println();

        Account account = new Account(1122,20000,0.045);
        account.deposit(1000);
        CheckAccount checkAccount1 = new CheckAccount(3000);
        checkAccount.withdraw(5000);//您所输入的钱数超过可透支额的限额!
        System.out.println(account);//Account{id=1122, balance=21000.0, annualInterestRate=0.045}
    }
}

img

麻烦大家帮忙看一下哪里出错了?

  • 写回答

1条回答 默认 最新

  • 笑死鱼啦 2022-05-29 19:40
    关注

    不知道你说的是不是运行出来第二行的余额是0
    因为你要取出的金额大于余额了呀, 账户余额就是0了, 而且还用了一部分透支额度

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月7日
  • 已采纳回答 5月30日
  • 创建了问题 5月29日

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。