java 重写其他类的问题

eagi6jfj  于 2023-02-11  发布在  Java
关注(0)|答案(2)|浏览(108)

当我尝试为类"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(双精度)不兼容
覆盖帐户.撤销
我不知道该修什么。

p8ekf7hl

p8ekf7hl1#

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

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

rqenqsqc2#

Account类中,您创建了返回类型为double的方法withdraw(),但在CheckingAccount类中,您使用返回类型void覆盖此方法。
Java 中,您不能更改覆盖方法中的返回类型,因为编译器不了解您要使用的方法。

相关问题