欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实验:继承&super

程序员文章站 2024-02-26 22:02:28
...

免责声明

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

题目要求

实验:继承&super实验:继承&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("超过可透支限额!");
		}
	}
}
相关标签: # 30天搞定Java