Java Collection framework, Arraylist working with objects



这里是开始分支类:

package com.sherzod;
import java.util.ArrayList;
public class Branch {
private String branchName;
private ArrayList<Customer> customer;
public Branch(String branchName) {
this.branchName = branchName;
this.customer = new ArrayList<>();
}
public String getBranchName() {
return branchName;
}
public ArrayList<Customer> getCustomer() {
return customer;
}
**public Customer findCustomer(String name){
for (int i=0; i<this.customer.size(); i++){
Customer checkedCustomer = this.customer.get(i);
if(checkedCustomer.equals(name)){
return checkedCustomer;
}
}
return null;
}**
}

这是开始客户类:

package com.sherzod;
import java.util.ArrayList;
public class Customer {
private String customerName;
private ArrayList<Double> transactions;
public Customer(String customerName) {
this.customerName = customerName;
this.transactions = new ArrayList<>();
}
public String getCustomerName() {
return customerName;
}
public ArrayList<Double> getTransactions() {
return transactions;
}
public void addTransaction(double amount){
transactions.add(amount);
}
}

问题是我们如何创建一个返回(作为布尔值)类型"客户"对象的方法? 即使我没有从分支扩展客户类,代码仍然可以工作,没有错误。 所以意味着客户与分支类有关系,因为我正在分支类中初始化客户数组列表?我从 8 个月开始学习 java,现在非常困惑..

对于包中的 java 类,您不需要在同一包中导入其他公共类。 即Customer类不需要导入到Branch类中(因为两者都是com.sherzod的一部分),不像ArrayListjava.util包的一部分。

为:

even if I didn't extend Customer class from Branches still code works, no errors. 

继承是一个不同的主题。关系 b/w 分支和客户类是它们在同一个包中,它们不需要有父子关系

最新更新