Java之实验(继承&super)

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

package com.atguigu.exer1;

public class Account {
	private int id;
	private double balance;
	private double annualInterestRate;//年利率
	
	public Account() {
		super();
	}
	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 getMonthlyIneterest() {
		return annualInterestRate / 12;
	}
	//取钱
	public void withdraw(double amount) {
		if(balance < amount) {
			System.out.println("余额不足!");
		}else {
			balance -= amount;
		}
	}
	//存钱
	public void deposit(double amount) {
		balance += amount;
	}
}

CheckAccount类

package com.atguigu.exer1;

public class CheckAccount extends Account{
	private double overdraft;//可透支额度
	
	public CheckAccount() {
		super();
	}

	public CheckAccount(int id,double balance,double annualInterestRate,double overdraft) {
		super(id,balance,annualInterestRate);
		this.overdraft = overdraft;
	}
	public void withdraw(double amount) {
		if(getBalance() >= amount) {
			super.withdraw(amount);
			System.out.println("取款成功");
			System.out.println("目前账户余额有" + getBalance());
			System.out.println("您的透支额度还剩:" + overdraft);
		}else if(amount < (overdraft + getBalance())) {
			overdraft = (overdraft + getBalance() - amount);
			setBalance(0);
			System.out.println("您的透支额度还剩:" + overdraft);
			System.out.println("目前账户余额为" + getBalance());
			setBalance(0);
		}else {
			System.out.println("您的透支额度已限额");
			System.out.println("您目前的透支额度还有" + overdraft);
			System.out.println("目前账户余额有" + getBalance());
			System.out.println("您目前最多能取走的金额为" + (overdraft + getBalance()));
		}
	}
}

Accout和CheackAccount的测试类

package com.atguigu.exer1;

public class AccountTest {
	public static void main(String[] args) {
		Account acc = new Account(1122, 20000, 0.045);
		acc.withdraw(30000);
		System.out.println("您的账户余额为" + acc.getBalance());
		acc.deposit(3000);
		acc.withdraw(2500);
		System.out.println("\n您的账户余额为" + acc.getBalance());
		System.out.println("月利率为" + acc.getMonthlyIneterest() * 100 + "%");
		
		System.out.println("*************************");
		CheckAccount ca = new CheckAccount(1122, 20000, 0.045, 5000);
		ca.withdraw(5000);
		System.out.println("\n");
		ca.withdraw(18000);
		System.out.println("\n");
		ca.withdraw(3000);
	}
}

调试结果
在这里插入图片描述

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页