[英]Can we call a Arraylist from a class to another class, and can be used by all methods in the 'another' class? No static - Java
[英]Call for methods of an ArrayList in another class in Java
如果我有三个班级。 一类是A
,一类是Customer
,我在构造函数中放置了A
的ArrayList
,还有一类是SavingsAccount
,我想将它们放在一起。 在A
类中A
我有一个方法,我想从SavingsAccount
调用。 我怎么做? 并同时调用Customer
和SavingsAccount
? 在Customer
有一个变量; SocSecNr
,必须与SavingsAccount
中的Nr
匹配,才能在A
正确,这就是我将SavingsAccount
的ArrayList
放在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
来在A
和C
之间建立关联。 您可以在构造函数中传递对它的引用或使用 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.