在另一个类java中调用arraylist的方法

时间:2014-05-07 08:01:54

标签: java class methods arraylist callback

如果我有三节课。一个A类,一个类Customer,我在构造函数中放置了A的arraylist,以及一个我希望将它们放在一起的SavingsAccount类。在A类中,我有一个方法,我想从SavingsAccount调用。我怎么做?并且要求客户和SavingsAccount? 在客户中有一个变量;必须与SavingsAccount中的Nr匹配的SocSecNr在A中是正确的,这就是为什么我在Customer中放置了SavingsAccount的arraylist。 (这只是一个示例类。我只想知道如何在没有继承的情况下执行此调用)

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;

}
}

2 个答案:

答案 0 :(得分:0)

你可以通过双向实现它

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();
}

答案 1 :(得分:0)

在A类中,我有一个方法,我想从C

调用

您应该通过将A字段放入C来在CA之间建立关联。您可以在构造函数中传递对它的引用,或使用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;

  }
}