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());
}
}
不知道你这题是这意思不,不用基础知识好几年了,继承多态都快忘差不多了,测试方法里用第一个父类引用指向子类对象的,符合这题的考点