[英]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.