Java中使用toString方法的问题

Java中使用toString方法的问题,java,inheritance,tostring,Java,Inheritance,Tostring,我正在开发一个银行程序,该程序应该使用3个类(Account基类、CheckingAccount和SavingsAccount)和几种方法来显示银行信息(ID、提款和存款后的余额以及年利率)。这应该是一个非常简单的程序,但我对其中的一个部分感到不安。在2个子类(CheckingAccount和SavingsAccount)中,我们被告知“调用它们的toString()方法”,我们简要地谈到了类中的重写和toString()方法的主题,但是我们的教科书中并没有太多关于这个主题的内容,我很难弄清楚如

我正在开发一个银行程序,该程序应该使用3个类(Account基类、CheckingAccount和SavingsAccount)和几种方法来显示银行信息(ID、提款和存款后的余额以及年利率)。这应该是一个非常简单的程序,但我对其中的一个部分感到不安。在2个子类(CheckingAccount和SavingsAccount)中,我们被告知“调用它们的toString()方法”,我们简要地谈到了类中的重写和toString()方法的主题,但是我们的教科书中并没有太多关于这个主题的内容,我很难弄清楚如何在我的特定程序中实现这一点

这是我到目前为止所编写的代码(由于我仍在尝试将toString()方法放在其中,所以还没有测试它是否有效,但除此之外,我认为它几乎完成了)

这是我的CheckingAccount类:

public class CheckingAccount extends Account {
  Account checkingAccount = new Account(1271, 150);
  checkingAccount.setAnnualInterestRate(1.25);
  checkingAccount.withdrawal(300);
  checkingAccount.deposit(500);
  double overdraft = 200;

  public String toString() {
    return 
  }
public class SavingsAccount extends Account {
    Account savingsAccount = new Account(1122, 20000);
    savingsAccount.setAnnualInterestRate(4.5);
    savingsAccount.withdrawal(5000);
    savingsAccount.deposit(10000);

}
这是我的储蓄账户课程:

public class CheckingAccount extends Account {
  Account checkingAccount = new Account(1271, 150);
  checkingAccount.setAnnualInterestRate(1.25);
  checkingAccount.withdrawal(300);
  checkingAccount.deposit(500);
  double overdraft = 200;

  public String toString() {
    return 
  }
public class SavingsAccount extends Account {
    Account savingsAccount = new Account(1122, 20000);
    savingsAccount.setAnnualInterestRate(4.5);
    savingsAccount.withdrawal(5000);
    savingsAccount.deposit(10000);

}
如果有帮助,最终输出应该如下所示:

Account Created!
Savings Account ID: 1122 Balance: $20000.00 Rate: 4.5%
Checking Account ID: 1271 Balance: $150.00 Rate: 1.25%
Updating Interest
Savings Account ID: 1122 Balance: $20900.00 Rate: 4.5%
Checking Account ID: 1271 Balance: 151.88 Rate: 1.25%
Processing Withdrawals
Savings Account ID: 1122 Balance: $15900.00 Rate: 4.5%
Checking Account ID: 1271 Balance: $... Rate: 1.25$ Overdraft
Processing Deposits
Savings Account ID: 1122 Balance:... Rate: 4.5%
Checking Account ID: 1271 Balance:... Rate: 1.25%
Thank you for your business!
如有任何建议,将不胜感激

更新 toString()方法看起来像下面这样吗

public String toString() {
    return ID: + id + Balance: + balance + Rate: + annualInterestRate;
  }
我想真正让我困惑的是如何使用两个toString()方法(每个子类一个)来打印输出,就像示例中所展示的那样

更新 这是我最新的代码,我仍然会遇到一些编译器错误,所有这些错误都涉及从静态上下文调用的非静态变量(我还会在代码后面发布这些错误)。直到上周中旬,我们才将所有内容声明为静态,但情况发生了变化,我很难确定在声明方法时何时使用静态以及何时不使用静态,因此出现了编译器错误

有一次我被告知,用更新后的代码替换原来的代码会使答案看起来很糟糕,所以我只是添加了我的当前代码,我希望这样可以

import java.util.Date;

public class Account {
    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private Date dateCreated = new Date();

    public Account() {
      id = 0;
      balance = 0.0;
      annualInterestRate = 0.0;
    } 

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

    public int getId() {
      return id;
    }

    public double getBalance() {
      return balance;
    }

    public double getAnnualInterestRate() {
      return annualInterestRate;
    }

    public void setId(int newId) {
      id = newId;
    }

    public void setBalance(double newBalance) {
      balance = newBalance;
    }

    public void setAnnualInterestRate(double newAnnualInterestRate) {
      annualInterestRate = newAnnualInterestRate;
      balance = balance * annualInterestRate;
    }

    public java.util.Date getDateCreated() {
      return dateCreated;
    }

    public double getMonthlyInterestRate() {
      return annualInterestRate/12;
    }

    public double withdrawal (double withdrawalAmount) {
      return balance -= withdrawalAmount;
    }

    public double deposit (double depositAmount) {
      return balance += depositAmount;
    }

  public static void main(String[] args) {
    Account savingsAccount = new Account(1122, 20000);
    Account checkingAccount = new Account(1271, 150);
    System.out.println("Accounts Created!");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.setAnnualInterestRate(4.5);
    checkingAccount.setAnnualInterestRate(1.25);
    System.out.println("Updating Interest");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.withdrawal(5000);
    checkingAccount.withdrawal(300);
    System.out.println("Processing Withdrawal");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.deposit(10000);
    checkingAccount.deposit(500);
    System.out.println("Processing Deposit");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    System.out.println("Thank you for your business!");
    }
  }

public class CheckingAccount extends Account {
  public static void main(String[] args) {
    //double overdraft = 200;
  }
    @Override
    public String toString() {
      return "Checking Account: ID: " + Account.getId() + "Balance: " + Account.getBalance() + "Rate: " + Account.getAnnualInterestRate();
    } 
} 

public class SavingsAccount extends Account {
  public static void main(String[] args) {
  }
    @Override
    public String toString() {
      return "Savings Account: ID: " + Account.getId()+ "Balance: " + Account.getBalance() + "Rate: " + Account.getAnnualInterestRate();
    } 
} 
以下是我收到的编译器错误:

Compilation completed.  The following files were not compiled:
6 errors found:
File: C:\Users\HiTechRedneck\Desktop\Summer II 2014\Computer Programming Principle\Program 5\CheckingAccount.java  [line: 7]
Error: non-static method getBalance() cannot be referenced from a static context
File: C:\Users\HiTechRedneck\Desktop\Summer II 2014\Computer Programming Principle\Program 5\CheckingAccount.java  [line: 7]
Error: non-static method getAnnualInterestRate() cannot be referenced from a static context
File: C:\Users\HiTechRedneck\Desktop\Summer II 2014\Computer Programming Principle\Program 5\SavingsAccount.java  [line: 6]
Error: non-static method getId() cannot be referenced from a static context
File: C:\Users\HiTechRedneck\Desktop\Summer II 2014\Computer Programming Principle\Program 5\SavingsAccount.java  [line: 6]
Error: non-static method getBalance() cannot be referenced from a static context
File: C:\Users\HiTechRedneck\Desktop\Summer II 2014\Computer Programming Principle\Program 5\SavingsAccount.java  [line: 6]
Error: non-static method getAnnualInterestRate() cannot be referenced from a static context
发件人:

每个类都有对象作为超类

java.lang.Object
类中有方法。这意味着您创建的每个类都可以使用该方法,即使它没有被重写(它是继承的)。打印对象时,它会打印该方法的返回值,
toString()

例如,当你写这篇文章时:

Account account1 = new Account();
System.out.println(account1);
与此相同:

Account account1 = new Account();
System.out.println(account1.toString());
讲师希望您做的是覆盖自动继承的
toString()
方法,以便您可以自定义打印对象时发生的情况

例如(将其添加到您的
帐户
类中):

通常
toString()
方法(如果未被覆盖)会打印以下内容:

getClass().getName() + '@' + Integer.toHexString(hashCode())
但现在您已经覆盖了它,它应该打印以下内容:

"ID: " + id + " " + "Balance: " + balance + " " + "Rate: " + annualInterestRate
发件人:

每个类都有对象作为超类

java.lang.Object
类中有方法。这意味着您创建的每个类都可以使用该方法,即使它没有被重写(它是继承的)。打印对象时,它会打印该方法的返回值,
toString()

例如,当你写这篇文章时:

Account account1 = new Account();
System.out.println(account1);
与此相同:

Account account1 = new Account();
System.out.println(account1.toString());
讲师希望您做的是覆盖自动继承的
toString()
方法,以便您可以自定义打印对象时发生的情况

例如(将其添加到您的
帐户
类中):

通常
toString()
方法(如果未被覆盖)会打印以下内容:

getClass().getName() + '@' + Integer.toHexString(hashCode())
但现在您已经覆盖了它,它应该打印以下内容:

"ID: " + id + " " + "Balance: " + balance + " " + "Rate: " + annualInterestRate
发件人:

每个类都有对象作为超类

java.lang.Object
类中有方法。这意味着您创建的每个类都可以使用该方法,即使它没有被重写(它是继承的)。打印对象时,它会打印该方法的返回值,
toString()

例如,当你写这篇文章时:

Account account1 = new Account();
System.out.println(account1);
与此相同:

Account account1 = new Account();
System.out.println(account1.toString());
讲师希望您做的是覆盖自动继承的
toString()
方法,以便您可以自定义打印对象时发生的情况

例如(将其添加到您的
帐户
类中):

通常
toString()
方法(如果未被覆盖)会打印以下内容:

getClass().getName() + '@' + Integer.toHexString(hashCode())
但现在您已经覆盖了它,它应该打印以下内容:

"ID: " + id + " " + "Balance: " + balance + " " + "Rate: " + annualInterestRate
发件人:

每个类都有对象作为超类

java.lang.Object
类中有方法。这意味着您创建的每个类都可以使用该方法,即使它没有被重写(它是继承的)。打印对象时,它会打印该方法的返回值,
toString()

例如,当你写这篇文章时:

Account account1 = new Account();
System.out.println(account1);
与此相同:

Account account1 = new Account();
System.out.println(account1.toString());
讲师希望您做的是覆盖自动继承的
toString()
方法,以便您可以自定义打印对象时发生的情况

例如(将其添加到您的
帐户
类中):

通常
toString()
方法(如果未被覆盖)会打印以下内容:

getClass().getName() + '@' + Integer.toHexString(hashCode())
但现在您已经覆盖了它,它应该打印以下内容:

"ID: " + id + " " + "Balance: " + balance + " " + "Rate: " + annualInterestRate

啊,好的,我现在明白了。非常感谢你@没问题。啊,好的,我现在明白了。非常感谢你@没问题。啊,好的,我现在明白了。非常感谢你@没问题。啊,好的,我现在明白了。非常感谢你@Bethtaner没问题。哦,Beth,你必须摆脱所有这些方法声明的
static
。好吧,就像我刚才补充的问题一样,直到最近我们才声明所有东西都是静态的,我仍然在试图弄清楚什么时候使用它,什么时候不使用它。当一个类有非静态方法时,它们会对该类的对象进行操作。对于静态方法,不涉及特定对象。因此,引用i是没有意义的