这里是开始分班:
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);
}
}问题是,我们如何创建一个返回(作为布尔型)类型的"Customer“对象的方法?即使我没有从分支扩展Customer类,代码仍然有效,没有错误。这意味着客户与分支类有关系,因为我在分支类中初始化Customer?我从8个月以来一直在学习java,现在太混乱了..
发布于 2020-08-30 17:16:38
对于包中的java类,不需要在同一个包中导入其他公共类。也就是说,Customer类不需要在Branch类中导入(因为两者都是com.sherzod的一部分),与ArrayList不同,ArrayList是java.util包的一部分。
适用于:
even if I didn't extend Customer class from Branches still code works, no errors. 继承是一个不同的主题。关系b/w分支和客户类是在同一个包中,它们不需要有父级子关系
https://stackoverflow.com/questions/63659963
复制相似问题