java基础_day0022_java_关键字_Override__子类_父类_个人账户存钱_消费_的测试


账号类    :


package day08_06;

public class Account {
	private int id;
	
	private double balance;
	
	private  double annualInterstRate;

	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 getAnnualInterstRate() {
		return annualInterstRate;
	}

	public void setAnnualInterstRate(double annualInterstRate) {
		this.annualInterstRate = annualInterstRate;
	}

	public Account(int id, double balance, double annualInterstRate) {
		super();
		this.id = id;
		this.balance = balance;
		this.annualInterstRate = annualInterstRate;
	}
	
	
	
public Account() {
		super();
	}

//	当前月利率
	public  double  getMonthlyInterest(){
		return this.annualInterstRate/12;
	}
//	消费 
	public  void withdraw(double amount){
		if(this.balance>=amount){
			this.balance-=amount;
		}else{
			System.out.println("当前余额不足");
		}
	}
//	存钱
	public  void  deposit(double amount){
		this.balance+=amount;
	}
	
}


子类  : 重写父类方法  Override

package day08_06;

public class CheckAccount extends Account{
	
	private  double  overdraft;
	
	
	
	
	
	@Override
	public void withdraw(double amount) {
		
		if(amount<super.getBalance()){
			super.withdraw(amount);
		}else if(amount > super.getBalance()){
			if((amount-super.getBalance()) > overdraft ){
				System.out.println("超过可透支额度的限额!");
			}else{
				this.overdraft-=(amount-super.getBalance());
				super.setBalance(0);
			}
				
		}
	}
	
	





	public double getOverdraft() {
		return overdraft;
	}







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







	

}

测试父类的方法 

package day08_06;

public class TestAccount {
	public static void main(String[] args) {
		Account acc = new  Account(1000,20000,0.045);
		acc.deposit(30000);
		acc.withdraw(2000);
		System.out.println(acc.getBalance());
	}
}

测试重写后 子类的方法 

package day08_06;

public class TestCheckAccount {
	public static void main(String[] args) {
		CheckAccount ca=new CheckAccount();
		ca.setBalance(20000);
		ca.setAnnualInterstRate(0.045);
		ca.setId(1122);
		ca.setOverdraft(5000);
		
		ca.withdraw(5000);
		System.out.println(ca.getBalance()+":-可透支额度"+ca.getOverdraft());
		ca.withdraw(18000);
		System.out.println(ca.getBalance()+":-可透支额度"+ca.getOverdraft());
		ca.withdraw(3000);
		System.out.println(ca.getBalance()+":-可透支额度"+ca.getOverdraft());
		
		
		
		
		
		
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值