java chapter07-1

来源:互联网 发布:淘宝宝贝拍照要求 编辑:程序博客网 时间:2024/05/21 03:25
package bank;public class Bank {int saveMoney;int year;double interest;double interestRate = 0.29;public double computerInterest(){interest = year*interestRate*saveMoney;return interest;}public void setInterestRate(double rate){interestRate = rate;}}
package bank;public class BankOfDalian extends Bank{double year;//computerInterest的重写public double computerInterest(){super.year = (int)year;double r = year - (int)year;int day = (int)(r*1000);//super调用隐函数computerInterest()的方法double yearInterest = super.computerInterest();double dayInterest = day*0.00012*saveMoney;interest = yearInterest+dayInterest;System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",saveMoney,super.year,day,interest);return interest;}}

package bank;public class ConstructionBank extends Bank{double year;//computerInterest的重写public double computerInterest(){super.year = (int)year;double r = year - (int)year;int day = (int)(r*1000);//super调用隐函数computerInterest()的方法double yearInterest = super.computerInterest();double dayInterest = day*0.0001*saveMoney;interest = yearInterest+dayInterest;System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",saveMoney,super.year,day,interest);return interest;}}

package bank;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint amount = 8000;ConstructionBank bank1 = new ConstructionBank();bank1.saveMoney = amount;bank1.year = 8.236;bank1.setInterestRate(0.035);double interest1 = bank1.computerInterest();BankOfDalian bank2 = new BankOfDalian();bank2.saveMoney = amount;bank2.year = 8.236;bank2.setInterestRate(0.036);double interest2 = bank2.computerInterest();System.out.printf("两个银行利息相差%f元\n",interest2 - interest1);}}


原创粉丝点击