首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java集合框架,使用对象的Arraylist

Java集合框架,使用对象的Arraylist
EN

Stack Overflow用户
提问于 2020-08-30 17:04:29
回答 1查看 63关注 0票数 0

这里是开始分班:

代码语言:javascript
复制
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;
    }**
}

下面是“开始客户级”:

代码语言:javascript
复制
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,现在太混乱了..

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-30 17:16:38

对于包中的java类,不需要在同一个包中导入其他公共类。也就是说,Customer类不需要在Branch类中导入(因为两者都是com.sherzod的一部分),与ArrayList不同,ArrayListjava.util包的一部分。

适用于:

代码语言:javascript
复制
even if I didn't extend Customer class from Branches still code works, no errors. 

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63659963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档