给定帐户t,代码意味着为AccountA实例中的字段savingsAccount调用BankAccount方法getBalance。我尝试了各种执行此方法的方法。 主要是后续的变化:
帐户t =新帐户();
吨
t.setSavingsAccount(new BankAccount());
的BankAccount @ XXXXXX
t.savingsAccount.getBalance();
静态错误:帐户中的任何字段都没有名称“savingsAccount”
如何重写此代码,以便将savingsAccount视为字段? 是否有更好的方法从t?
中执行方法getBalance相关守则:
/**
* 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;
}
}
答案 0 :(得分:3)
savingsAccount
不是公共领域。您需要添加一个访问者方法(即getSavingsAccount()
或将其公开。
答案 1 :(得分:0)
正确的语法是
System.out.println(t.getSavingsAccount().getBalance());