簡體   English   中英

在 Java 中調用另一個類中的 ArrayList 的方法

[英]Call for methods of an ArrayList in another class in Java

如果我有三個班級。 一類是A ,一類是Customer ,我在構造函數中放置了AArrayList ,還有一類是SavingsAccount ,我想將它們放在一起。 A類中A我有一個方法,我想從SavingsAccount調用。 我怎么做? 並同時調用CustomerSavingsAccount Customer有一個變量; SocSecNr ,必須與SavingsAccount中的Nr匹配,才能在A正確,這就是我將SavingsAccountArrayList放在Customer (這只是一個示例類。我只想知道如何在沒有繼承的情況下進行此調用)

import java.util.ArrayList;

public class A {
private ArrayList<Customer> customerlist;
private SavingsAccount account;

public A() {
    customerlist = new ArrayList<Customer>();
    account = new SavingsAccount();
}

public boolean deposit(long pNr, int accountId, double amount)
{   
    for(int i = 0; i < customerlist.size(); i++)
    {
        if(customerlist.get(i).getPCode() == pNr)
        {
            account.transaction(amount);
        }        
    }
    return false;        
       
}
public double transaction(){

    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;
}




   public class Customer {
private long pNr;
private ArrayList<SavingsAccount> accounts;

public Customer(long pCode)
{
    pNr = pCode;
    accounts = new ArrayList<SavingsAccount>();
}

public ArrayList<SavingsAccount> getAccount(){
    return accounts;
}
}



public class SavingsAccount {
private double balance;

public SavingsAccount(){
    accountId = accountCount;
}

public double transaction(double amount){
    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;

}
}

你可以通過兩種方式實現它

1-繼承

class C extentds A{
 //all the public or protected attribues and methods are availble here 
} 

2- By 有關系。

class C{

   private A aType;

   aType.methode();
}

在 AI 類中有一個我想從 C 調用的方法

您應該通過將C字段放入A來在AC之間建立關聯。 您可以在構造函數中傳遞對它的引用或使用 setter 方法對其進行初始化。 其實在deposit方法的代碼中是不需要的,但是可以在這個方法中進行初始化,以便在其他方法中進一步引用或使用。

編輯:

import java.util.ArrayList;

public class A {
  private ArrayList<Customer> customerlist;
  private Customer customer;
  private SavingsAccount account;

  public A() {
      customerlist = new ArrayList<Customer>();
  }

  public boolean deposit(long pNr, int accountId, double amount)
  {   
      for(Customer customer : customerlist) {
          if(customer.getPCode() == pNr) {
            this.customer = customer; //initialize
            List<SavingsAccount> accounts = customer.getAccounts();
            for (SavingsAccount account : accounts) {
              if (account.getAccountId() == accountId){
                this.account = account; //initialize
                account.transaction(amount);
                return true;
              } 
            }
          }        
      }
      return false;            
  }

}

public class Customer {
  private long pNr;
  private ArrayList<SavingsAccount> accounts;

  public long getPCode(){
    return pNr;
  }

  public Customer(long pCode)
  {
    pNr = pCode;
    accounts = new ArrayList<SavingsAccount>();
  }

  public ArrayList<SavingsAccount> getAccounts(){
    return accounts;
  }
}



public class SavingsAccount {
  private double balance;
  private int accountId;

  public int getAccountId(){
    return accountId;
  }

  public SavingsAccount(int accountId){
    this.accountId = accountId;
  }

  public double transaction(double amount){
    if(amount < 0 && balance + amount < 0)
        return -0;
    else
        return balance += amount;

  }
}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM