在Java中使用toString方法的问题

时间:2014-07-28 00:33:04

标签: java inheritance tostring

我正在开发一个应该使用3个类(Account-base类,CheckingAccount和SavingsAccount)的银行程序以及几种显示银行信息的方法(ID,提款和存款后的余额,以及年利率)。这应该是一个非常简单的程序,但我已经挂了它的一部分。在2个子类(CheckingAccount和SavingsAccount)中,我们被告知"调用他们的toString()方法"我们在课堂上简要介绍了覆盖和toString()方法的主题,但在我们的教科书中并没有很多关于这个主题的内容,而且我在查找如何执行此操作时遇到了一些麻烦我的特定节目?

这是我到目前为止的代码(还没有对它进行测试,看它是否有效,因为我还在尝试获取toString()方法,但除此之外,我认为它&& #39;差不多完成了)。

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课程:

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

1 个答案:

答案 0 :(得分:4)

来自the docs

  

每个类都将Object作为超类。

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

例如,当你写下这个:

Account account1 = new Account();
System.out.println(account1);

它与此相同:

Account account1 = new Account();
System.out.println(account1.toString());

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

例如(将其添加到Account班级):

@Override
public String toString() {
    return "ID: "+id+" "+"Balance: "+balance+" "+"Rate: "+annualInterestRate;
}

通常toString()方法(如果重写)打印出来:

getClass().getName() + '@' + Integer.toHexString(hashCode())

但是现在你已经覆盖了它,它应该打印出来:

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