-3

Is the following code:

class FamilyMember{
   String name;
   String type;
}

class Family{
   FamilyMember father;
   FamilyMemeber mother;
   List<FamilyMember> children;
}

will produces the following relationship:  

 +------------------+      +------------------+  
 |     Father       |------|     Mother       |  
 +------------------+  ^   +------------------+  
                       |                  
                       |                  
                       |                  
                       |                  
                       |                    
                 --------------       
                 |            |                   
                 |            |                   
                 |            |                   
                 |            |                   
 +------------------+       +------------------+  
 |     Son          |       |    Daughter      |  
 +------------------+       +------------------+  

 

Since the Number of children are different in different family, therefore every family have its own family structure.

I want to make the class which can shows the above relationship with variable number of children.

4

4 回答 4

1
class FamilyMember{
   String name;
   String type;
}

class Family{
   FamilyMember father;
   FamilyMemeber mother;
   List<FamilyMember> children;
}
于 2013-04-23T08:50:31.960 回答
1

您只需要一个具有父/子关系的简单树。树中的每个节点都是一个人。

不要忘记这一点;

  1. 两个孩子可能只共享一个共同的父母,而不是两者。例如,一个男人可以和不同的女人生两个孩子,反之亦然。
  2. 所有的父母本身都是隐含的孩子。
  3. 每个孩子将有两个父母,而不是正常的一个

具有以下数据的类就足够了;

public class Person {

  boolean female;
  Person father;
  Person mother;
  List<Person> children;

}
于 2013-04-23T08:56:26.943 回答
0
class Parent
{
    String name;
    String gender;
}

class Family
{
    Map<Set<Parent, Parent>, String> child;
}
于 2013-04-23T09:07:00.177 回答
0

为什么不创建一个名为 Family 的类?可以有多个(n)个孩子和1个或0个(悲伤的世界:/)父亲和母亲。

您还可以将每个孩子与特定的母亲和父亲联系起来。

实际上我宁愿选择一个有性别的类 Person 并且所有其他类都应该从它继承。由于每个人都有很多东西,比如名字和东西,从长远来看,这应该更容易处理......

于 2013-04-23T08:54:44.507 回答