如何将对象添加到另一个对象集中

时间:2010-08-23 06:34:43

标签: java object set

我有两节课。一个(Person)用于getter和setter,另一个(People)用于计算数据。我的情况是,我使用ResultSet从数据库获取数据,然后创建一个人物对象来存储行数据。然后我创建了人物对象来存储所有人。

每个对象创建为SET。

while(rs.next())
{
    Set<People> people = new HashSet<people>();
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

现在问题是While循环people.add(person);

中的最后一行

它说

  

类型Set中的方法add(People)不适用于参数(Person)

我如何克服这个问题?

感谢。

6 个答案:

答案 0 :(得分:8)

我的设计理解是,您有人有多人关系,因此People类包含Person个对象的集合。然后我会期待这样的事情:

public class Person {
  private String name;
  private Date dateOfBirth;
  // .. more attributes

  // getters and setters

  // overrides of equals, hashcode and toString
}

public class People implements Set<Person> {
  private Set<Person> persons = new HashSet<Person>();

  public boolean add(Person person) {
    return persons.add(person);
  }

  // more methods for remove, contains, ...
}

因此,在您的数据库相关代码中,您不需要创建另一个集合,因为People已经拥有您需要的集合:

People people = new People();  // or get it, if it's already created
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

答案 1 :(得分:2)

我不明白为什么你首先想要2个班级。你也有人实现计算部分。但是,你可以做些什么:

class People implements Set<Person> {

private HashSet<Person> hashset = new HashSet<Person>();

// ... your computational code goes here
// delegate all Set methods to hashset
}

然后:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.add(person);
}
return people;

答案 2 :(得分:1)

根据您的尝试,我觉得,您应该在添加到集合之前将Person转换为People类。您的People类可能有一个构造函数,该构造函数将Person作为参数并将所需字段从Person复制到People。您要添加到集合中的代码将显示为people.add(new People(person));

当你声明Set<People> people = new HashSet<People>();时,这个集合应该包含'type'对象的对象,即People的实例或People的子类的实例。如果People是一个接口,那么该集合可能包含实现该接口的任何对象。

答案 3 :(得分:1)

我理解Person是一个数据结构(类似于bean,带有getter和setter),People应包含数据库中的所有Person个对象,并对它们执行计算。 / p>

如果这是真的,首先,你不能在循环中声明人(因为将为每个People创建一个新的Person对象,你不希望这样,我理解。)

其次,People需要能够包含Person对象。所以它至少应该由SetPerson个对象组成。您可以根据需要添加更多功能。所以,尝试这样的事情:

public class People {

    Set<Person> persons = new HashSet<Person>();

    Set<Person> getPersons() {
        return persons;
    }

    int computeSomethingAboutPeople() { 
        // return as you please
    }

}

像这样使用它,如上一张海报所示:

People people = new People();
while(rs.next())
{
    Person person = new Person();
    String name = rs.getString(2);
    person.setName(name);
    int id = rs.getInt(1);
    person.setId(id);
    String dept = rs.getString(4);
    person.setDept(dept);
    int age = rs.getInt(3);
    person.setAge(age);
    people.getPersons().add(person);
}
int answer = people.computeSomethingAboutPeople();

答案 4 :(得分:0)

我认为不应该在循环中写入Set<People> people = new HashSet<people>();

答案 5 :(得分:0)

class Cartesian
{
   double x,y,z;
   public
   Cartesian()
   {
      x=y=z=0;
   }
   Cartesian (int i,int j,int k)
   {
        x=i;
        y=j;
        z=k;
   }

   Cartesian add_coordinates(Cartesian c)
   {
        c.x=x+c.x;
        c.y=y+c.y;
        c.z=z+c.z;      
        return c;                 
   }

   void display()
   {
        System.out.println("Addition of coordinates is : "+x+"i "+y+"j "+z+"k ");
   }

}

class Coordinate
{
  public static void main(String[] args)
  {
       Cartesian obj1 = new Cartesian(5,5,-10);
       Cartesian obj2 = new Cartesian(5,5,-10);
       Cartesian obj3 = new Cartesian();
       obj3=obj1.add_coordinates(obj2);
       obj3.display();
  }
}
相关问题