现在有一个银行类叫Bank
年利率 double lilv;
年限 int year;
存款金额 double money;
利息 double lixi;
计算利息的方法computerInterest(),返回利息。利息公式=存款金额*年限*利率
设置利率方法 setLilv(double lilv)
现在有一个子类 建设银行ConstructionBank继承Bank
年限 double year; (说明:这个年限不是整年,里面包含了天数,天数由小数点后的数字表示,最大就是0.364。比如 3.126,表示3年零126天。)
天利率:double daylilv=0.0001;
对父类中计算利息的方法computerInterest()进行重写:
比如年限是3.123,前面的整数年按照父类中的年利率方法计算[先将整数年取出,赋值给父类的year,即super.year=xxx,然后调用父类的计算利息的方法,super. computerInterest()],后面的123天按照子类中的天利率来计算(天数*daylilv*存款金额),返回他们的和。
另一个子类 重庆银行 ChongqingBank 继承Bank
年限 double year; (说明:这个年限不是整年,里面包含了天数,天数由小数点后的数字表示,最大就是0.364。比如 3.126,表示3年零126天。)
天利率:double daylilv=0.0002;
对父类中计算利息的方法computerInterest进行重写:
比如年限是3.123,前面的整数年按照父类中的年利率方法计算[先将整数年取出,赋值给父类的year,即super.year=xxx,然后调用父类的计算利息的方法,super. computerInterest()],后面的123天按照子类中的天利率来计算(天数*daylilv*存款金额),返回他们的和。
要求,在主类中创建ConstructionBank的实例化对象cbank,将存款金额设置为80000,设置利率为0.035,设置year为8.168。计算利息并输出。
创建重庆银行的实例化对象cqBank,将存款金额设置为80000,设置利率为0.042,设置year为8.168。计算利息并输出。
package shiyan_08;
import java.text.*;//java.text提供以与自然语言无关的方式来处理文本、日期、数字和消息的类和接口。
public class Mainclass {
public static void main(String[] agrs) {
ConstructionBank cbank ;
cbank = new ConstructionBank(8.168, 80000);
//不同声明方式
ChongqingBank cqBank = new ChongqingBank(8.168, 80000);
cbank.setLilv(0.035);
cqBank.setLilv(0.042);
System.out.println("建设银行的利息为:" + new DecimalFormat("0").format(cbank.computerInterest()));// DecimalFormat将数字格式化
System.out.println("重庆银行的利息为:" + new DecimalFormat("0").format(cqBank.computerInterest()));// 四舍五入取整数
System.out.printf("建设银行的利息为:%d",cqBank.computerInterest());//错误的语句
}
}
package shiyan_08;
public class Bank {
double lilv;// 年利率
int year;// 年限
double money;// 存款金额
double lixi;// 利息
public Bank(int year, double money) {
this.year = year;
this.money = money;
}
public double computerInterest() {
return money * year * lilv;
}
public void setLilv(double lilv) {
this.lilv = lilv;
}
}
package shiyan_08;
public class ConstructionBank extends Bank {
public ConstructionBank(double year, double money) {
super(0, money);
this.year = year;
}
double year;
double daylilv = 0.0001;
public double computerInterest() {
super.year = (int) year;
return ((year - super.year) / 0.001 * daylilv * money) + super.computerInterest();
}
}
package shiyan_08;
public class ChongqingBank extends Bank {
public ChongqingBank(double year, double money) {
super(0, money);
this.year = year;
}
double year;
double daylilv = 0.0002;
public double computerInterest() {
super.year = (int) year;
super.lixi = super.computerInterest();
return ((year - super.year) / 0.001 * daylilv * money) + super.computerInterest();
}
}