Java继承与多态(抽象类和接口练习)定义抽象类Bank,它包含oneyearRate(一年定期利率)oneyearNationalbebt(一年国债利率)和CurrentDepositRate(按年

定义抽象类Bank,它包含oneyearRate(一年定期利率)oneyearNationalbebt(一年国债利率)和CurrentDepositRate(按年计算的活期利率)三个利息率常数(分别为1.75%,2.0%,0.35%),本金Principal=1000, 设计 count()方法及show()方法。count()方法用于计算存款一年所的利息及缴利息税后的总金额(原存款额+税后利息),而show()方法用于显示总金额。
由于定期存款、活期存款和国债利率不同,要求定义3个类,分别表示这3种储蓄。3个类都继承抽象类Bank,实现Bank中的count()及show()抽象方法,完成对利息和总金额的计算和显示功能(本金1000元)。
注意:除国债外,定期存款和活期存款利息都要交纳20%的个人所得税。
源代码

abstract public class Bank {
	double oneyearRate=0.0175;			//一年定期利率
	double oneyearNationalbebt=0.02;	//一年国债利率
	double CurrentDepositRate=0.0035;	//按年计算的活期利率
	double Principal=1000;				//本金
	
	abstract double count();			//计算存款一年所得利息及缴利息税后的总金额
	abstract void show();				//显示总金额
}

public class CurrentDeposit extends Bank{
	double count(){
		double interest;
		interest=(1-0.2)*CurrentDepositRate;
		return Principal*(1+interest);
	}
	void show() {
		System.out.println("定期存款后总金额:"+count());
	}
}

public class Test {
	static Bank[] deposit= {new TimeDeposit(),new CurrentDeposit(),new TreasuryRates()};
	public static void main(String[] args) {
		for(int i=0;i<deposit.length;i++) {
			deposit[i].show();
		}
	}
}

public class TimeDeposit extends Bank{
	double count(){
		double interest;
		interest=(1-0.2)*oneyearRate;
		return Principal*(1+interest);
	}
	void show() {
		System.out.println("定期存款后金额:"+count());
	}
}

public class TreasuryRates extends Bank{
	double count(){
		return Principal*(1+oneyearNationalbebt);
	}
	void show() {
		System.out.println("国债后金额:"+count());
	}
}

运行结果
在这里插入图片描述
(2)将bank改为接口重新完成

public interface Bank {
	double oneyearRate=0.0175;			//一年定期利率
	double oneyearNationalbebt=0.02;	//一年国债利率
	double CurrentDepositRate=0.0035;	//按年计算的活期利率
	double Principal=1000;				//本金
	
	double count();						//计算存款一年所得利息及缴利息税后的总金额
	void show();						//显示总金额
}


public class CurrentDeposit implements Bank{
	public double count(){
		double interest;
		interest=(1-0.2)*CurrentDepositRate;
		return Principal*(1+interest);
	}
	public void show() {
		System.out.println("定期存款后金额:"+count());
	}
}


public class Test {
	static Bank[] deposit= {new TimeDeposit(),new CurrentDeposit(),new TreasuryRates()};
	public static void main(String[] args) {
		for(int i=0;i<deposit.length;i++) {
			deposit[i].show();
		}
	}
}


public class TimeDeposit implements Bank{
	public double count(){
		double interest;
		interest=(1-0.2)*oneyearRate;
		return Principal*(1+interest);
	}
	public void show() {
		System.out.println("定期存款后金额:"+count());
	}
}


public class TreasuryRates implements Bank{
	public double count(){
		return Principal*(1+oneyearNationalbebt);
	}
	public void show() {
		System.out.println("国债后金额:"+count());
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值