【问题标题】:Call for methods of an ArrayList in another class in Java在 Java 中调用另一个类中的 ArrayList 的方法
【发布时间】:2014-06-24 02:15:28
【问题描述】:

如果我有三门课。一类A,一类Customer,我在构造函数中放置了AArrayList,还有一类SavingsAccount,我想将它们放在一起。在课堂上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;

}
}

【问题讨论】:

  • 类和属性是不同的东西
  • 你的类名应该以大写字母开头,这样代码更容易理解。
  • 那么,在 A 中你有一个 B 元素的 ArrayList,还是一些通用的 type 的 ArrayList?如果是这样,为什么你把它称为 b,作为另一个类的名称?
  • 这些不是我的实际课程,只是一个例子。我想知道如何从a类进入c的方法的原理。我已经尝试使用 c.callThisMethod(x) 但只得到错误。
  • @Keppil 我改进了我的代码,希望现在更容易理解。

标签: java class methods arraylist callback


【解决方案1】:

你可以通过两种方式实现它

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

【讨论】:

  • 您的解决方案没有考虑 ArrayList 的东西。
  • 你可以说private List aList;
【解决方案2】:

在 A 类中,我有一个我想从 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;

  }
}    

【讨论】:

  • 请看我更新的代码。也许我不清楚类之间的关系。我假设您的意思是将 B 和 C 的对象放在 A 类中? A 中的 Arraylist 包含 B 的对象,B 中的 Arraylist 包含 C 的对象
  • 如果arraylists有引用那么你可以使用它们,否则你必须获取它们或者创建一个新的。
  • 我知道问题可能很简单,但我只是不知道当它们之间有一个类时如何使用它们。如何在 A 中调用 C 中的方法?
  • 我不认为我理解正确,但是“在 A 中”您应该获得对 C 的引用 value。如果您将引用作为依赖项提供,这很容易创建时注入到 A 中。而且,您可以稍后在运行时提供它。但我认为您不仅需要引用,还需要数组内的对象,该字段应满足搜索条件。然后您搜索 B 然后在 B 中搜索 C 并返回 B 和 C 的引用,您可以使用它们来初始化 A。
  • 更新了答案,我想你应该如何以及为什么要创建类属性已经很清楚了。
猜你喜欢
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
相关资源
最近更新 更多