实验:继承&super

免责声明

此项目为尚硅谷官网提供的学习资料,转载仅为方便学习,官方链接

题目要求

在这里插入图片描述在这里插入图片描述

项目实现

我的答案

我的答案

参考答案

package com.atguigu.exer2;

public class Account {
	private int id;//账号
	private double balance;//余额
	private double annualInterestRate;//年利率
	
	public Account(int id, double balance, double annualInterestRate) {
		super();
		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;
			return;
		}
		System.out.println("余额不足");
	}
	//存钱
	public void deposit (double amount){
		if(amount > 0){
			balance += amount;
		}
	}
}
package com.atguigu.exer2;

public class CheckAccount extends Account {

	private double overdraft;// 可透支限额

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

	public double getOverdraft() {
		return overdraft;
	}

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

	@Override
	public void withdraw(double amount) {
		if (getBalance() >= amount) {// 余额就足够消费
//			getBalance() -= amount;
			// 方式一:
//			setBalance(getBalance() - amount); 
			// 方式二:
			super.withdraw(amount);
		} else if (overdraft >= amount - getBalance()) {// 透支额度+余额足够消费

			overdraft -= (amount - getBalance());

//			setBalance(0);
			// 或
			super.withdraw(getBalance());

		} else {
			System.out.println("超过可透支限额!");
		}
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值