覆盖另一个类的问题

时间:2018-03-28 17:05:14

标签: java encoding balance

当我尝试为类" CheckingAccount创建一个新的withdraw方法时出于某种原因我收到此错误。"我还有一个名为Account的类,它有自己的withdraw方法。

以下是代码:

    class CheckingAccount extends Account {
    double overdraftmax = -50;

    public CheckingAccount(int id, double balance) {
    }
    public void withdraw(double money) {
            if (this.getBalance() - money >= overdraftmax) {
                withdraw(money);
    }
  }
}

class Account {
    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private java.util.Date dateCreated;


    Account() {
        dateCreated = new java.util.Date();
    }

    Account(int newId,double newBalance) {
        this();
        id = newId;
        balance = newBalance;

    }
    int getId() {
        return id;
    }
    double getBalance() {
        return balance;
    }
    double getAnnualInterestRate() {
        return annualInterestRate;
    }
    void setId(int newId) {
        id = newId;
    }
    void setBalance(double newBalance) {
        balance = newBalance;
    }
    void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }
    String getDateCreated() {
        return dateCreated.toString();
    }   
    double getMonthlyInterestRate() {
            return (annualInterestRate / 100) / 12;
    }
    double getMonthlyInterest() {
            return balance * getMonthlyInterestRate();
    }
    double withdraw(double money) {
            return balance -= money;
    }
    double deposit(double money) {
            return balance += money;
    }
}

以下是我遇到的两个错误。

  

返回类型与Account.withdraw(double)

不兼容      

覆盖Account.withdraw

我不知道该修复什么。

2 个答案:

答案 0 :(得分:0)

当覆盖方法时,你需要在父方法中保留相同的方法原型。 所以在这里你混合了返回类型。

class CheckingAccount extends Account {
        double overdraftmax = -50;

        public CheckingAccount(int id, double balance) {
        }
        public double withdraw(double money) {
                if (this.getBalance() - money >= overdraftmax) {
                    withdraw(money);
        }
      return "double var";
  }
}


class Account {
    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private java.util.Date dateCreated;


    Account() {
        dateCreated = new java.util.Date();
    }

    Account(int newId,double newBalance) {
        this();
        id = newId;
        balance = newBalance;

    }
    int getId() {
        return id;
    }
    double getBalance() {
        return balance;
    }
    double getAnnualInterestRate() {
        return annualInterestRate;
    }
    void setId(int newId) {
        id = newId;
    }
    void setBalance(double newBalance) {
        balance = newBalance;
    }
    void setAnnualInterestRate(double newAnnualInterestRate) {
        annualInterestRate = newAnnualInterestRate;
    }
    String getDateCreated() {
        return dateCreated.toString();
    }   
    double getMonthlyInterestRate() {
            return (annualInterestRate / 100) / 12;
    }
    double getMonthlyInterest() {
            return balance * getMonthlyInterestRate();
    }
    double withdraw(double money) {
            return balance -= money;
    }
    double deposit(double money) {
            return balance += money;
    }
}

答案 1 :(得分:0)

Account类中,您使用返回类型withdraw()创建方法double,但在类CheckingAccount中,您使用返回类型void覆盖此方法。

Java 中,您无法在覆盖方法中更改返回类型,因为编译器无法理解您要使用的方法。