1条回答 默认 最新
- Gemini_Kanon 2021-06-21 15:19关注
public class Account { private String idCard; private double balance; public Account() { } public Account(String idCard, double balance) { this.idCard = idCard; this.balance = balance; } public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } @Override public String toString() { return "Account{" + "idCard='" + idCard + '\'' + ", balance='" + balance + '\'' + '}'; } public double getInterest(){ return 0.0; } }
public class FixedDepositAccount extends Account { private int months; private double rate; public FixedDepositAccount() { } public FixedDepositAccount(String idCard, double balance) { super(idCard, balance); } public FixedDepositAccount(String idCard, double balance, int months, double rate) { super(idCard, balance); this.months = months; this.rate = rate; } public int getMonths() { return months; } public void setMonths(int months) { this.months = months; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } @Override public String toString() { return super.getIdCard() + "账户" + super.getBalance() + "元存款的" + this.getMonths() + "月存款利息:" + (super.getBalance() * this.getRate() * this.getMonths() / 12) + "(年利率为3.50%)"; } @Override public double getInterest() { return super.getInterest(); } }
public class BankingAccount extends Account { private int days; private double rate; public BankingAccount() { } public BankingAccount(String idCard, double balance) { super(idCard, balance); } public BankingAccount(String idCard, double balance, int days, double rate) { super(idCard, balance); this.days = days; this.rate = rate; } public int getDays() { return days; } public void setDays(int days) { this.days = days; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } @Override public String toString() { return super.getIdCard() + "账户" + super.getBalance() + "元存款的" + this.getDays() + "天存款利息:" + (super.getBalance() * this.getRate() * this.getDays() / 365) + "(年利率为5.20%)"; } @Override public double getInterest() { return super.getInterest(); } }
public class Test { public static void main(String[] args) { //父类引用指向子类对象 Account fda = new FixedDepositAccount("001", 10000, 6, 3.5); Account ba = new BankingAccount("002", 10000, 183, 5.2); System.out.println(fda.toString()); System.out.println(ba.toString()); //正常创建对象 // FixedDepositAccount fda = new FixedDepositAccount("001", 10000, 6, 3.5); // BankingAccount ba = new BankingAccount("002", 10000, 183, 5.2); // System.out.println(fda.toString()); // System.out.println(ba.toString()); } }
不知道你这题是这意思不,不用基础知识好几年了,继承多态都快忘差不多了,测试方法里用第一个父类引用指向子类对象的,符合这题的考点
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
- ¥20 OPENVPN连接问题
- ¥15 flask实现搜索框访问数据库
- ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
- ¥100 如何用js写一个游戏云存档
- ¥15 ansys fluent计算闪退
- ¥15 有关wireshark抓包的问题
- ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
- ¥15 向数据表用newid方式插入GUID问题
- ¥15 multisim电路设计