【问题标题】:Object Oriented Java: External method calling with variable reference面向对象的Java:使用变量引用调用外部方法
【发布时间】:2020-02-23 11:45:24
【问题描述】:

使用 BlueJ 作为编译器来学习外部方法调用/引用。我的目标是了解更多关于从另一个类调用方法以及如何从该类引用变量的信息。

我的理解是外部方法调用本质上是这样的:

object.methodName(parameters);

我正在进行的当前项目要求我通过ArrayList 并提取列表中所有帐户的余额。我只和两个班级一起工作。 Bank.classBankAccount.classBankAccount.class我有4个方法; withdraw()deposit()showBal()acctInfo()。对于银行,我只有两种方法,listAllAccounts()sumAllAccounts()

我一直坚持为sumAllAccounts() 编写代码,因为它要求我对添加到private ArrayList<BankAccount> accounts 的所有BankAccount.class 对象的余额求和

我不需要一个直截了当的答案,我只是在这里,因为我已经用尽了我的资源。在我的课程中,我们还没有讨论过static classesnull,但是很多类似的帖子我都很难理解。

我在别处读到,不可能将实例变量从一个类带到另一个类。所以我很好奇我应该如何将float 类型变量提取到另一个类中进行计算。

到目前为止我写的是:

private void sumAllAccounts()
{
   int index = 0;
   float bal = 0;
   float sum = 0;
   while(index < accounts.size()) {
      accounts.get(index);
      bal = XXXXXXXXXXXXXXXXXXXXXXXXX
      sum = sum+bal;
      index++;
   }
   System.out.println("The total balance of all accounts listed are: " + sum);
}

我知道 balance 应该在那一刻查看索引对象并提取我们可以添加到总和的余额。我不知道如何提取这种平衡。再一次,这只是我正在研究的一个例子,以便我可以更好地解释自己。您的回复的上下文可能不太具体。

【问题讨论】:

  • account.get(i).showBal() ?
  • 你可以试试bal = account.get(index).showBal() 吗?我不知道showBal() 的返回类型是否为Float

标签: java class variables methods external


【解决方案1】:

假设showBal()BankAccount 中的float 返回一个float

public void sumAllAccounts() {
    float sum1 = 0;
    for(BankAccount bankAccount : accounts) {
        sum1 += bankAccount.showBal();
    }
    System.out.println("The total balance of all accounts listed are: " + sum1);

    // or
    int index = 0;
    float bal = 0;
    float sum2 = 0;
    while(index < accounts.size()) {
        bal = accounts.get(index).showBal();
        sum2 = sum2 + bal; // can be simplified to: sum2 += accounts.get(index).showBalance();
        index++;
    }
    System.out.println("The total balance of all accounts listed are: " + sum2);

    // or
    float sum3 = 0;
    for(int i = 0; i < accounts.size(); i++) {
        sum3 += accounts.get(i).showBal();
    }
    System.out.println("The total balance of all accounts listed are: " + sum3);
}

【讨论】:

    【解决方案2】:

    好吧,您的 Bankaccount 类中有一个名为 showBal() 的方法,并假设它返回一个浮点数:

    // first get() the account that you are working on
    BankAccount currentAccount = account.get(i);
    
    // then invoke the method to retrieve the balance
    bal = currentAccount.showBal();
    

    您还应该使用 for 循环而不是 while 循环:

    float bal = 0;
    float sum = 0;
    
    for (int index = 0; index < account.size(); index++){
    
       BankAccount currentAccount = account.get(i);
       bal = currentAccount.showBal();
       sum += bal; // "sum += bal;" is a shorter way of coding "sum = sum + bal;"
    
    }
    
    System.out.println("The total balance of all accounts listed are: " + sum);
    

    这样,您不必声明索引整数并使用索引++。它都包含在 for 循环中!

    【讨论】:

      【解决方案3】:
      int index = 0;
      while(index < accounts.size()) {
          sum += accounts.get(index).showBal();
          index++;
      }
      

      虽然当列表大小固定时,我更喜欢使用 for 循环

      for(int i = 0; i < accounts.size(); i++) {
          sum += accounts.get(i).showBal();
      }
      

      或一个for-each

      for(BankAccount bankAccount : accounts) {
          sum += bankAccount.showBal();
      }
      

      一旦你理解了循环,你也可以尝试用更优雅的 Lambdas 来做:

      float sum = accounts.stream().reduce(0.0, (subtotal, element) -> subtotal + element.showBal());
      

      【讨论】:

        猜你喜欢
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多