开发者

Calling Methods from embedded classes in Java

Given Account t, code is meant to enable BankAccount method g开发者_如何学PythonetBalance to be called for the field savingsAccount within an instance of Account. I've tried various methods of executing this method. Mainly variations of the follwing:

Account t = new Account();

t

t.setSavingsAccount(new BankAccount());

BankAccount@xxxxxx

t.savingsAccount.getBalance();

Static Error: No field in account has name "savingsAccount"

how do i rewrite this code so that savingsAccount is considered a field? is there a better way to execute the method getBalance from within t?

Relevant Code:

    /**
 * A class to track an individuals bank portfolio.
 */
public class Account extends Object{
  private BankAccount savingsAccount = null;              //savings account identification number

 /**
   * A method to set account holder's savings account identification number.
   * @param savings account identification number
   * @return savings account identification number
   */
  public BankAccount setSavingsAccount(BankAccount input){
    savingsAccount = input;
    return savingsAccount;
  }

     /**
      * A method to return account holder's savingsaccount identification number.
      * @return savingsaccount identification number
      */
     public BankAccount getSavingsAccount(){
        return savingsAccount;
     }
    }

/**
 * A class to track the balance and terms of a bank account.
 */
public class BankAccount extends Object{
  private double currentBalance = 0;              //the balance of the account

 /**
  * Returns the balance of the account.
  * @param - void
  * @return the balance of the account
  */
  public double getBalance(){
    return currentBalance;
  }
    }


savingsAccount is not a public field. You need to either add an accessor method (i.e. getSavingsAccount() or make it public.


The correct syntax is

System.out.println(t.getSavingsAccount().getBalance());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜