Java如何用两个类创建一组对象?

Java如何用两个类创建一组对象?,java,object,set,Java,Object,Set,我需要理解一个逻辑问题,即如何创建一组私有的居民对象。这是我的两门课: package main; import java.util.Set; /** * @author * @version 0.1 * */ public class City { private String name; /** *HERE is my Problem*/ private Set<Inhabitants> Inhabitants {

我需要理解一个逻辑问题,即如何创建一组私有的居民对象。这是我的两门课:

package main;
import java.util.Set;


/**
 * @author 
 * @version 0.1
 *
 */
public class City {

    private String name;
    /**
    *HERE is my Problem*/   
    private Set<Inhabitants> Inhabitants {

    } 

    /**
     * standard cunstructor
     */
    public City(String n, ) {
        setName(n);
    }

    /**
     * Search a particular name of inhabitant
     * @return object of inhabitant
     */
    static Set<Inhabitants> Search(){
        return inhabitants;

    }

    /**
     * Creates a inhabitant object and 
     * add to the set of inhabitants of the city
     * 
     * @param name
     * @param datebirth
     * @param maritalStatus
     * @
     */
    public void Add(String name,String datebirth,String maritalStatus) {

    }
    /**
     * Return all inhabitants objects of the city
     * @return 
     */
    static Set<Inhabitants> ReturnAll(){
        return inhabitants; 
    }

    /**
     * Return city name
     * @return 
     */
    static Set<Inhabitants> ReturnCityName(){
        return inhabitants;
    }


    /**
     * @return the name
     */
    public String getName() {
    return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the inhabitants
     */
    public Set<Inhabitants> getInhabitants() {
        return inhabitants;
    }

    /**
     * @param inhabitants the inhabitants to set
     */
    public void setInhabitants(Set<Inhabitants> inhabitants) {
        this.inhabitants = inhabitants;
    }
}

package main;

/**
 * @version 0.1
 */
public class Inhabitants {

    private String name;
    private String datebirth;
    private Boolean maritalStatus;

    /**
     * standard constructor
     */
    public Inhabitants() {
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the datebirth
     */
    public String getDatebirth() {
        return datebirth;
    }

    /**
     * @param datebirth the datebirth to set
     */
    public void setDatebirth(String datebirth) {
        this.datebirth = datebirth;
    }

    /**
     * @return the maritalStatus
     */
    public Boolean getMaritalStatus() {
        return maritalStatus;
    }

    /**
     * @param maritalStatus the maritalStatus to set
     */
    public void setMaritalStatus(Boolean maritalStatus) {
        this.maritalStatus = maritalStatus;
    }
}
packagemain;
导入java.util.Set;
/**
*@作者
*@version 0.1
*
*/
公营城市{
私有字符串名称;
/**
*这是我的问题
私人居住者{
} 
/**
*标准结构器
*/
公共城市(字符串n,){
集合名(n);
}
/**
*搜索居民的特定姓名
*@居民的返回对象
*/
静态集搜索(){
返回居民;
}
/**
*创建居住对象并
*增加城市居民的数量
* 
*@param name
*@param dateborning
*@param maritalStatus
* @
*/
public void Add(字符串名称、字符串日期出生、字符串状态){
}
/**
*归还城市的所有居民和物品
*@返回
*/
静态集合ReturnAll(){
返回居民;
}
/**
*返回城市名称
*@返回
*/
静态集合ReturnCityName(){
返回居民;
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@返回居民
*/
公共集{
返回居民;
}
/**
*@param居住者-居住者设置
*/
公共空间集合居民(集合居民){
这个。居民=居民;
}
}
主包装;
/**
*@version 0.1
*/
公共阶层居民{
私有字符串名称;
私生子;
私人身份;
/**
*标准构造器
*/
公共居民(){
}
/**
*@返回名称
*/
公共字符串getName(){
返回名称;
}
/**
*@param name要设置的名称
*/
公共void集合名(字符串名){
this.name=名称;
}
/**
*@返回出生日期
*/
公共字符串getDatebirth(){
返回日期出生日期;
}
/**
*@param datebirth要设置的日期出生
*/
public void setDatebirth(字符串datebirth){
this.datebirth=datebirth;
}
/**
*@返回maritalStatus
*/
公共布尔值getMaritalStatus(){
返回maritalStatus;
}
/**
*@param maritalStatus要设置的maritalStatus
*/
public void setMaritalStatus(布尔maritalStatus){
this.maritalStatus=maritalStatus;
}
}

这听起来像个简单的问题。您不需要这么多代码:

package collections;

/**
 * Person
 * @author Michael
 * @link  https://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
 * @since 6/20/2015 5:06 PM
 */
public class Person {
    private final String name;

    public Person(String name) {
        if (name == null || name.trim().length() == 0) throw new IllegalArgumentException("name cannot be blank or null");
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        return name.equals(person.name);

    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Person{");
        sb.append("name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

package collections;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * City
 * @author Michael
 * @link  https://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
 * @since 6/20/2015 5:06 PM
 */
public class City {

    private Set<Person> inhabitants;

    public City(Collection<Person> inhabitants) {
        this.inhabitants = (inhabitants == null) ? new HashSet<Person>() : new HashSet<Person>(inhabitants);
    }

    public void addInhabitant(Person p) {
        if (p != null) {
            this.inhabitants.add(p);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("City{");
        sb.append("inhabitants=").append(inhabitants);
        sb.append('}');
        return sb.toString();
    }
}
package集合;
/**
*人
*@作者迈克尔
*@linkhttps://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
*@自2015年6月20日下午5:06起
*/
公共阶层人士{
私有最终字符串名;
公众人物(字符串名称){
如果(name==null | | name.trim().length()==0)抛出新的IllegalArgumentException(“name不能为空或null”);
this.name=名称;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
人=(人)o;
返回name.equals(person.name);
}
@凌驾
公共int hashCode(){
返回name.hashCode();
}
@凌驾
公共字符串toString(){
最终StringBuilder sb=新StringBuilder(“人员{”);
sb.append(“name=”).append(name).append(“\”);
某人附加('}');
使某人返回字符串();
}
}
包裹收集;
导入java.util.Collection;
导入java.util.HashSet;
导入java.util.Set;
/**
*城市
*@作者迈克尔
*@linkhttps://stackoverflow.com/questions/30958832/java-how-create-a-set-of-object-with-two-classes
*@自2015年6月20日下午5:06起
*/
公营城市{
私有居民;
公共城市(收集居民){
this.incidents=(incidents==null)?new HashSet():new HashSet(incidents);
}
公共无效添加物(p人){
如果(p!=null){
本条增补(p);
}
}
@凌驾
公共字符串toString(){
最终StringBuilder sb=新StringBuilder(“城市{”);
某人追加(“居民”)。追加(居民);
某人附加('}');
使某人返回字符串();
}
}

您真的需要发布您的全部代码吗?你的例子不能简化吗?还要使用编辑器菜单中的
{}
按钮格式化代码。到底是什么问题?哪两门课你有问题?但是对于这样一个简单的类,我们也不需要所有的代码在
城市中
?我必须从类居住者创建一个类型为Set的私有属性。但我不明白这一点。我在书中搜索了一些例子,什么也没找到。对不起,我的大脑是空的……我不知道什么是“私人属性”。你能告诉我们你想做什么吗?City这个类应该如何在外部工作?那么,如果两个人的名字相同,那么他们是平等的?另外,我必须在Javadoc中查看哪一章?它被称为一个简单的、自包含的示例。您需要设置语义的等于和哈希代码。适应你认为合适的。