如果我有三堂课。一类是A,一类是Customer,其中我在构造函数中放置了A的数组列表,一类是SavingsAccount,我想将它们组合在一起。在A类中,我有一个要从SavingsAccount调用的方法。我怎么做?并同时要求客户和SavingsAccount?
在客户中有一个变量; SocSecNr,必须与SavingsAccount中的Nr匹配,才能在A中正确,这就是为什么我在客户中放入了SavingsAccount的数组列表的原因。
(这只是一个示例类。我只想知道如何在不继承的情况下进行此调用)

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-通过有关系。

class C{

   private A aType;

   aType.methode();
}

09-16 03:29