java类的继承问题,如下代码,为什么我用子类的构造器创建子类对象用父类的构造器创建父类对象,子类获得到的父类的余额属性是0呢?
package B_five.A;
/**
* @author MollieZhang
* @create 2022-05-29 14:37
*/
public class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public Account() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", balance=" + balance +
", annualInterestRate=" + annualInterestRate +
'}';
}
public boolean withdraw(double amount){
if (amount >= 0.0 && amount <= balance){
balance-=amount;
System.out.println("成功取出"+amount+"CNY。"+"您当前的余额为"+balance+"CNY");
return true;
}else{
System.out.println("输入数值不合法!");
return false;
}
}
public boolean deposit(double amount){
if (amount >=0.0){
balance+=amount;
System.out.println("成功存入"+amount+"CNY。"+"您当前的余额为"+balance+"CNY");
return true;
}else{
System.out.println("输入数值不合法!");
return false;
}
}
}
package B_five.A;
/**
* @author MollieZhang
* @create 2022-05-29 14:41
*/
public class CheckAccount extends Account{
private double overdraft;
public CheckAccount(double overdraft) {
this.overdraft = overdraft;
}
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {
super(id, balance, annualInterestRate);
this.overdraft = overdraft;
}
@Override
public String toString() {
return super.toString()+"CheckAccount{" +
"overdraft=" + overdraft +
'}';
}
@Override
public boolean withdraw(double amount) {
if (amount >= 0 && amount <= super.getBalance()){
super.setBalance(super.getBalance()-amount);
System.out.println("成功取钱 "+amount+"CNY。您当前的余额为 "+super.getBalance()+"CNY。"+
"您当前的可透支余额为 "+overdraft+" CNY。");
return true;
}else if(amount > super.getBalance()){
if ((amount - super.getBalance() <= overdraft)){
overdraft-=amount - super.getBalance();
super.setBalance(0);
System.out.println("成功取钱 "+amount+"CNY。您当前的余额为 "+super.getBalance()+"CNY。"+
"您当前的可透支余额为 "+overdraft+" CNY。");
return true;
}else{
System.out.println("您所输入的钱数超过可透支额的限额!");
return false;
}
}
return false;
}
}
package B_five.A;
/**
* @author MollieZhang
* @create 2022-05-29 15:02
*/
public class Test {
public static void main(String[] args) {
CheckAccount checkAccount = new CheckAccount(1122,20000,0.045,5000);
checkAccount.withdraw(5000);
checkAccount.withdraw(18000);
checkAccount.withdraw(3000);
System.out.println();
Account account = new Account(1122,20000,0.045);
account.deposit(1000);
CheckAccount checkAccount1 = new CheckAccount(3000);
checkAccount.withdraw(5000);//您所输入的钱数超过可透支额的限额!
System.out.println(account);//Account{id=1122, balance=21000.0, annualInterestRate=0.045}
}
}
麻烦大家帮忙看一下哪里出错了?