java - 在 Java 中使用 toString 方法的问题

标签 java inheritance tostring

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

7年前关闭。




Improve this question




我正在开发一个银行程序,该程序应该使用 3 个类(Account-base 类、CheckingAccount 和 SavingsAccount)和几种方法来显示银行信息(ID、取款和存款后的余额以及年利率) .这应该是一个非常简单的程序,但我对它的一部分感到困惑。在 2 个子类(CheckingAccount 和 SavingsAccount)中,我们被告知“调用他们的 toString() 方法” 我们在类里面简要介绍了覆盖和 toString() 方法的主题,但我们的教科书中并没有太多关于该主题的内容,并且我在弄清楚如何执行此操作时遇到了一些麻烦,因为它适用于我的特定程序?

这是我到目前为止的代码(尚未对其进行测试以查看它是否有效,因为我仍在尝试在其中获取 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 int setId(int newId) {
      id = newId;
    }

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

    public double setAnnualInterestRate(double newAnnualInterestRate) {
      annualInterestRate = newAnnualInterestRate;
    }

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

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

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

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

  public static void main(String[] args) {
    //System.out.println("Account Created!");
    //System.out.println("Savings Account: " + savingsAccount.getId() + "Balance: $%1.2f." + savingsAccoung.getBalance() + "Rate: " + savingsAccoung.getAnnualInterestRate() + "%");
    //System.out.println("Checking Account: " + checkingAccount.getId() + "Balance: $%1.2f." + checkingAccount.getBalance() + "Rate: " + checkingAccount.getAnnualInterestRate() + "%");

  }
}

这是我的 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 
  }

这是我的 SavingsAccount 类(class):
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

最佳答案

来自 the docs :

Every class has Object as a superclass.



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

例如,当你写这个时:
Account account1 = new Account();
System.out.println(account1);

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

你的导师希望你做的是覆盖 toString()自动继承的方法,以便您可以自定义打印出对象时发生的情况。

例如(将其添加到您的 Account 类(class)中):
@Override
public String toString() {
    return "ID: "+id+" "+"Balance: "+balance+" "+"Rate: "+annualInterestRate;
}

通常是 toString()方法(如果没有被覆盖)打印:
getClass().getName() + '@' + Integer.toHexString(hashCode())

但是现在你已经覆盖了它,它应该打印这个:
"ID: " + id + " " + "Balance: " + balance + " " + "Rate: " + annualInterestRate

关于java - 在 Java 中使用 toString 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24987061/

相关文章:

java - 从 map 对象中查找范围内的元素数

java - 无法在 Ubuntu 上使用 JDK 9 启动 Eclipse Neon - 退出代码 = 13

c++ - 为什么要使用虚拟基类?

java - SingleTable策略,如果子类中没有指定表名,会抛出异常吗?

javascript - 如何将整个文档 HTML 作为字符串获取?

java - 如何使用 lambda/java8 迭代递归列表

java - 如何在java中以HHhMM格式显示时间?

java - JSF 2 : Is it possible to inherit @ManagedBean?

c# - 为什么 Visual Studio 不警告我空引用异常?

Java:找不到符号