【问题标题】:Issues with overriding another class覆盖另一个类的问题
【发布时间】:2018-03-28 17:05:14
【问题描述】:

当我尝试为“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

我不确定要解决什么问题。

【问题讨论】:

  • 那么,错误信息中有什么不清楚的地方?
  • 你用另一个返回void的方法覆盖了一个返回double的方法,这是不允许的。要么让超类返回 void(恕我直言),要么在覆盖方法中返回 double
  • @AlexSavitsky 这行得通,谢谢!

标签: java encoding balance


【解决方案1】:

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

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;
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多