【问题标题】:Code returning node instead of value of subclass代码返回节点而不是子类的值
【发布时间】:2016-01-02 05:07:09
【问题描述】:

我对编程非常陌生,并且在我正在学习的课程中遇到了一些代码问题。我需要创建一个银行账户类来返回余额、添加存款、取款并打印当前利率的报表。我一直试图让主类返回储蓄账户类的值。相反,它返回节点值,我无法弄清楚我做错了什么。任何帮助,将不胜感激。代码如下。 FinalExam.java

    import java.io.*;
    import java.util.Scanner;


public class FinalExam
{
    SavingsAccount savings;
    static String name;
    static double balance;    

    public static void main (String[] args) throws NegativeAmountException, InsufficientFundsException {
    double amount = 0;
    SavingsAccount savings = null;
    Scanner keyboard = new Scanner(System.in);
   try
   {
       savings = new SavingsAccount("Penny Saved", 500.00, .05);
       System.out.println(savings);
   }
   catch(NegativeAmountException e)
   {
      System.out.println("NegativeAmountException: " + e.getMessage());
      System.exit(1);

   }

   System.out.println(savings);
   Scanner input = new Scanner(System.in);
   System.out.print("Enter your deposit amount: ");
   amount = keyboard.nextDouble();
   System.out.println(savings);
   System.out.println("Enter your withdrawal amount:  ");
   amount = keyboard.nextDouble();
   savings.postInterest();
   System.out.println(savings);
}
}

BankAccount.java

import java.util.InputMismatchException;
import java.util.Scanner; //Import for scanner


public class BankAccount {
  public String name;
  public double balance;     

  //set name and balance 
  // make sure balance is not negative
  // throw exception if balance is negative

  public BankAccount(String name, double balance)throws NegativeAmountException {
      if (balance < 0)
        throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");


  }

    public BankAccount(String name)
            throws NegativeAmountException 
  {
    // set name and use 0 balance 
        name = null;
        balance = 0;
  }


  public void deposit(double amount) throws NegativeAmountException {
      if (amount < 0)
        throw new NegativeAmountException("Cannot deposit a negative amount: " + amount);
      balance = balance + amount;
  }

  public void withdraw(double amount) throws InsufficientFundsException, NegativeAmountException {
    if (amount > balance)
        throw new InsufficientFundsException("Cannot withdraw more than the current balance of this BankAccount");
    if (amount < 0)
        throw new NegativeAmountException("Cannot withdraw a negative amount: " + amount);
    balance = balance - amount;
  }

  public double getBalance() {

    return balance;

  }
  // print bank statement including customer name
 // and current account balance

  public void printStatement() {
      System.out.println("BankAccount owned by: " + name +  "    balance: $" + balance);
    }
}

SavingsAcount.java

public class SavingsAccount extends BankAccount
{
    double interest;

    public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException {

      super(name, balance);


    }
    public double getInterest()
    {
    return interest;
    }
    public void postInterest() {

       balance = balance + ((balance * interest)/12);
    }

    public void printStatement() {
        super.printStatement();
      System.out.println("Account for " + name +  " Saved" + balance + "    balance: $" + "Current Interest Rate is" + interest);
      }
 }

我知道 FinalExam.java 的最后一部分是不完整的,但我试图在处理其他问题之前打印出声明。任何建议将不胜感激。

【问题讨论】:

  • 您遇到了什么问题?
  • 除了其他答案之外,您没有在 SavingsAccount 构造函数中设置兴趣字段。在super(name, balance);之后,添加this.interest = interest;
  • BankAccount 构造函数也是如此;字段未设置。
  • @WilliamCallahan 不要在 cmets 中使用标签。对于代码,把它放在``.看起来like this
  • @WilliamCallahan 你说对了。这解决了我的最后一个问题。我觉得我现在很好。

标签: java class inheritance


【解决方案1】:

我一直试图让主类返回储蓄账户类的值。相反,它返回节点值,我无法弄清楚我做错了什么。

你的问题是你在打电话

savings = new SavingsAccount("Penny Saved", 500.00, .05);
System.out.println(savings);

println 在其参数上自动调用toString 方法。由于您没有 @Override toStringSavingsAccount 方法,因此它会打印您所谓的“节点值”。

你的SavingsAccount 有一个方法

public void printStatement()

这似乎打印了您想要的信息。所以调用它而不是println

【讨论】:

  • 就是这样,我知道我错过了一些简单的东西。我认为我可以使用帮助的最后一个问题是,现在我调用了正确的方法,它只返回利率,而不是账户名称和余额。相反,它返回的 BankAccount 拥有者为:null balance: $0.0Current Interest Rate0.05。有什么建议吗?
  • 我实际上是从其他 cmets 那里得到的。感谢您的帮助。
  • @Probowlactic 查看您正在调用的构造函数。你调用SavingsAccount(String, double, double),除了调用超级构造函数BankAccount(String, double,它什么都不做。您还需要调用这些类中的方法,而不仅仅是构造函数。
【解决方案2】:

在 FinalExam.java 类中,您想打印 SavingsAccount.java 类的值,但您得到的是地址。这个 id 是因为在 FinalExam.java 类 System.out.println(savings); 中,而不是你应该像 savings.printStatement(); 这样使用。可能我猜的没错..

【讨论】:

    【解决方案3】:

    当您使用System.out.println(savings) 方法打印对象时,它会在内部调用该对象的toString() 方法。您需要为 SavingsAccountBankAccount 类实现 toString() 方法。

    对于SavingsAccount,请使用:

    @Override
    public String toString() {
      return "SavingsAccount{" +
          super.toString() +
          ", interest=" + interest +
          '}';
    }
    

    BankAccount 使用这个:

    @Override
    public String toString() {
      return "BankAccount{" +
          "name='" + name + '\'' +
          ", balance=" + balance +
          '}';
    }
    

    目前您没有在类中设置实例变量。您正在检查负余额并抛出异常。 您还需要改进BankAccount 类中的构造函数,如下所示:

    public BankAccount(String name, double balance)throws NegativeAmountException {
      if (balance < 0) {
        throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");
      }
      this.name = name;
      this.balance = balance;
    }
    // Update the SavingsAccount class constructor as well
    public SavingsAccount(String name, double balance, double interest) throws NegativeAmountException {
      super(name, balance);
      this.interest = interest;  // this is missing in your code.
    }
    

    我观察到的其他事情是,您正在创建 Scanner 类的多个实例。您可以在 main 方法的每个位置使用相同的 keyboard 实例。无需创建多个实例。其中之一是不必要的。

    Scanner keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    

    【讨论】:

      【解决方案4】:

      答案已经给出,但是你的BankAccount类构造函数也有问题,你没有将实例变量分配给构造函数参数,你只是检查约束,

      public BankAccount(String name, double balance)throws NegativeAmountException {
            if (balance < 0)
              throw new NegativeAmountException("Cannot create a BankAccount with a negative balance");
            this.name = name;
            this.balance = balance;
      }
      

      public BankAccount(String name) {
          // set name and use 0 balance 
              this.name = name;
              this.balance = 0;   
      }
      

      谢谢:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多