Java集合框架,ArrayList与对象一起使用。

huangapple go评论72阅读模式
英文:

Java Collection framework, Arraylist working with objects

问题

以下是已经翻译好的内容:

这是起始的分支(Branch)类:

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;
    }
}

这是起始的顾客(Customer)类:

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"对象的方法?
即使我没有从Branch类继承Customer类,代码仍然可以工作,没有错误。
这是否意味着Customer与Branch类有关系,因为我在Branch类中初始化了Customer的ArrayList?
我学习Java已经8个月了,现在感到非常困惑...

英文:

Here is starting Branch Class:

package com.sherzod;

import java.util.ArrayList;

public class Branch {
    private String branchName;
    private ArrayList&lt;Customer&gt; customer;

    public Branch(String branchName) {
        this.branchName = branchName;
        this.customer = new ArrayList&lt;&gt;();
    }

    public String getBranchName() {
        return branchName;
    }

    public ArrayList&lt;Customer&gt; getCustomer() {
        return customer;
    }

    **public Customer findCustomer(String name){
        for (int i=0; i&lt;this.customer.size(); i++){
            Customer checkedCustomer = this.customer.get(i);
            if(checkedCustomer.equals(name)){
                return checkedCustomer;
            }
        }
        return null;
    }**
}

Here is starting Customer Class:

package com.sherzod;

import java.util.ArrayList;

public class Customer {
    private String customerName;
    private ArrayList&lt;Double&gt; transactions;

    public Customer(String customerName) {
        this.customerName = customerName;
        this.transactions = new ArrayList&lt;&gt;();
    }

    public String getCustomerName() {
        return customerName;
    }

    public ArrayList&lt;Double&gt; getTransactions() {
        return transactions;
    }

    public void addTransaction(double amount){
        transactions.add(amount);
    }
}

question is how come we create a method which is return (as a boolean) type "Customer" object?
even if I didn't extend Customer class from Branches still code works, no errors.
so means Customer has a relationship with Branch class because of I am initializing Customer Arraylist in the Branch class? I am learning java since 8 month so much confusing now..

答案1

得分: 0

对于同一个包中的 Java 类,您无需导入该包中的其他公共类。
Customer 类无需在 Branch 类中导入(因为两者都是 com.sherzod 的一部分),不像 ArrayList 属于 java.util 包。

对于:

即使我没有将 Customer 类从 Branch 类继承,代码仍然可以工作,没有错误。

继承是一个不同的主题。Branches 和 Customer 类之间的关系是它们位于同一个包中,它们不需要有父子关系。

英文:

For a java class in a package you don't need to import the other public classes in the same package.
i.e. Customer class doesn't need to be imported in Branch class (as both are part of com.sherzod), unlike ArrayList which is part of java.util package.

For:

even if I didn&#39;t extend Customer class from Branches still code works, no errors. 

Inheritance is a different subject. Relation b/w Branches and Customer classes is that they are in the same package, they don't need to have a parent child relation

huangapple
  • 本文由 发表于 2020年8月31日 01:04:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63659963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定