简体   繁体   English

类运算符错误数组:所需的意外类型:找到的变量:值

[英]Array of Class Operator Error: unexpected type required: variable found: value

I have an array of the BankAccount class and nested classes, including AccountInfoPrv with the method getAcctBalance(). 我有一个BankAccount类和嵌套类的数组,包括带有方法getAcctBalance()的AccountInfoPrv。 The error occurs at the last line, where I call the BankAccount method getAccountInfoPrv() which then goes into the AccountInfoPrv class to call getAcctBalance(). 错误发生在最后一行,在这里我调用BankAccount方法getAccountInfoPrv(),然后进入AccountInfoPrv类以调用getAcctBalance()。 It returns whatever value is there at index i, i've tested this without the operator in main method and it returns the value perfectly.. i'm not sure what i did wrong here. 它返回索引i处存在的任何值,我已经在main方法中没有使用运算符的情况下对此进行了测试,并且它完美地返回了该值。.我不确定我在这里做错了什么。

Also, there is a lot of other code involved in this method but i tried to simply it with just this for loop. 另外,此方法涉及很多其他代码,但我尝试仅使用此for循环将其简化。

  public static void withdrawal(Scanner kybd, BankAccount[] 
  account, int num_accts)
 {

 double amountToWithdraw;
 amountToWithdraw = kybd.nextDouble();

 for(int i=0; i<num_accts; i++)
 account[i].getAccountInfoPrv().getAcctBalance() -= 
 amountToWithdraw;
 }

Expected to subtract withdrawal amount from the value of account[i] and set account[i] to the new value. 期望从account [i]的值中减去提款金额,并将account [i]设置为新值。

Output: Error: unexpected type required: variable found : value 输出:错误:所需的意外类型:找到变量:值

What you are trying to do over here is modify the "Value" returned by the getter method. 您要在此处进行的操作是修改getter方法返回的“值”。 In order to perform any such operation on the value returned by getter method, you would 为了对getter方法返回的值执行任何此类操作,您需要

  1. First need to store it into a variable, 首先需要将其存储到变量中,
  2. and modify its value, 并修改其价值,
  3. and then call the setter to set the updated value in the object. 然后调用设置器以在对象中设置更新的值。

ie

balance = account[i].getAccountInfoPrv().getAcctBalance();
balance -= amountToWithdraw;
account[i].getAccountInfoPrv().setAcctBalance(balance);

In case of non-primitive type, calling setter explicitly is not required as it would get updated via reference. 如果是非原始类型,则不需要显式调用setter,因为它将通过引用进行更新。

Here you try to change from a getter (the method) it is not allowed. 在这里,您尝试从不允许的吸气剂(方法)进行更改。 Try 尝试

account[i].getAccountInfoPrv().setAcctBalance(account[i].getAccountInfoPrv().getAcctBalance() - amountToWithdraw);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM