我有一个逻辑问题需要理解,如何创建一个私有的居民对象集。这是我的两个班级:
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;
}
}
答案 0 :(得分:1)
这听起来像一个简单的问题。你并不需要这么多代码:
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();
}
}