MollieZhang 2022-05-29 15:57 采纳率: 100%
浏览 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日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么