1

目前,我正在尝试用 Java 对家谱应用程序的一些值进行硬编码。

只是需要一些关于如何去做的想法,不知道如何进行。

我还包括了构造函数类:

  public class TreeImpl {

    private FamilyMember root;

    public FamilyMember getRoot() {
        return root;
    }

    public void setRoot(FamilyMember root) {
        this.root = root;
    }

    public List<FamilyMember> getAllMembers() {
        return allMembers;
    }

    public void setAllMembers(List<FamilyMember> allMembers) {
        this.allMembers = allMembers;
        this.setRoot(this.allMembers.get(0));
    }

    private List<FamilyMember> allMembers = new ArrayList<>();

    public TreeImpl(FamilyMember root)
    {
        this.root=root;
        addMembers(root);
    }

    private void addMembers(FamilyMember node)
    {
        if(node==null) return;
        allMembers.add(node);
        addMembers(node.getFather());
        addMembers(node.getMother());
        addMembers(node.getSpouse());


        for(FamilyMember child : node.getChildren())
            addMembers(child);

    }

    public void addSpouse(int index, FamilyMember spouse)
    {
        for(FamilyMember member : allMembers)
        {
            if(member.getMemberID()==index)
            {
                member.setSpouse(spouse);
                allMembers.add(spouse);
                return;
            }
        }
    }

    public void addFather(int index, FamilyMember father)
    {
        for(FamilyMember member : allMembers)
        {
            if(member.getMemberID()==index)
            {
                member.setFather(father);
                allMembers.add(father);
                return;
            }
        }
    }

    public void addMother(int index, FamilyMember mother)
    {
        for(FamilyMember member : allMembers)
        {
            if(member.getMemberID()==index)
            {
                member.setMother(mother);
                allMembers.add(mother);
                return;
            }
        }
    }

    public void addChild(int index, FamilyMember child)
    {
        for(FamilyMember member : allMembers)
        {
            if(member.getMemberID()==index)
            {
                member.setSpouse(child);
                allMembers.add(child);
                return;
            }
        }
    }



    public FamilyMember getDetailsForMember(String member)
    {

        for(FamilyMember m : this.getAllMembers())
        {
            if(m.getFirstName().equals(member))
                return m;
        }

        return null;
    }

我的构造函数类

public class FamilyMember implements Serializable{
    private static int id=1;

    private String firstName, surName, surNameAfterMarriage, life;
    private Gender gender;
    private Address address;
    private FamilyMember mother, father, spouse;
    private int memberID;

    public boolean hasSub()
    {
        return (this.getFather()!=null || this.getMother()!=null || this.getChildren().size()>0);
    }


    public int getMemberID() {
        return memberID;
    }

    public void setMemberID(int memberID) {
        this.memberID = memberID;
    }

    private List<FamilyMember> children, grandChildren;

    public static int getId() {
        return id;
    }

    public static void incrementId()
    {
        id++;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public String getSurNameAfterMarriage() {
        return surNameAfterMarriage;
    }

    public void setSurNameAfterMarriage(String surNameAfterMarriage) {
        this.surNameAfterMarriage = surNameAfterMarriage;
    }

    public String getLife() {
        return life;
    }

    public void setLife(String life) {
        this.life = life;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public FamilyMember getMother() {
        return mother;
    }

    public void setMother(FamilyMember mother) {
        this.mother = mother;
    }

    public FamilyMember getFather() {
        return father;
    }

    public void setFather(FamilyMember father) {
        this.father = father;
    }

    public FamilyMember(String firstName, String surName, String surNameAfterMarriage, String life, Gender gender,
            Address address) {
        super();
        this.firstName = firstName;
        this.surName = surName;
        this.surNameAfterMarriage = surNameAfterMarriage;
        this.life = life;
        this.gender = gender;
        this.address = address;
        this.memberID = this.getId();
        this.children = new ArrayList<>();
        this.incrementId();
    }


        for(FamilyMember child : this.getChildren())
        {
            text = text + " " + child.firstName;
        }

        return text;
    }

    public FamilyMember getSpouse() {
        return spouse;
    }

    public void setSpouse(FamilyMember spouse) {
        this.spouse = spouse;
    }

    public List<FamilyMember> getChildren() {
        return children;
    }

    public void setChildren(List<FamilyMember> childer) {
        this.children = childer;
    }

    public void addChild(FamilyMember child) {
        this.children.add(child);
    }


    public List<FamilyMember> getGrandChildren() {
        return grandChildren;
    }

    public void setGrandChildren(List<FamilyMember> grandChildren) {
        this.grandChildren = grandChildren;
    }


}

例如,我想为父亲、母亲、孩子等添加详细信息,所以当我运行程序时,会显示这些值

例如:Familymember father = new Familymember ("xyz". "xyx" )

4

3 回答 3

0

在 FamilyMember 类中创建一个静态方法来创建一个族,然后在初始化 TreeImpl 对象之前调用此方法。

public static FamilyMember createFirstFamilyNode() {
    FamilyMember mother = new FamilyMember("Mother", ...);
    FamilyMember father = new FamilyMember("Father", ...);

    mother.setSpouce(father);
    FamilyMember child1 = new FamilyMember("First Child", ...);
    mother.addChild(child1);

    // And so on for rest of family members

    return mother;
}
于 2018-05-21T14:35:18.730 回答
0

您可以使用构造函数设置值,

public FamilyMember (Gender gen,Address add,FamilyMember fmm,FamilyMember fmf,FamilyMember fms,int mId){
            this.gender = gen;
            this.address = add;
            this.mother = fmm;
            this.father = fmf;
            this.spouse = fms;
            this.memberID = mId;
        }

它将使用硬编码值设置 FamilyMember 对象,

FamilyMember fm = new FamilyMember(Gender.MALE,add,fmm,fmf,fms,menId);

如果有任何疑问,请告诉我。

于 2018-05-21T10:27:00.300 回答
0

如果你真的想通过构造函数来实现它,你可以这样做:

// Secondary constructor
public FamilyMember() {
    this("Paul", "Stevenson", ...); // calls the primary constructor
}

public FamilyMember(String firstName, String surName, String surNameAfterMarriage, String life, Gender gender,
            Address address) {
    // your primary constructor
}

不幸的是,这不是很灵活。另一种方法是使用静态方法:

public static FamilyMember familyMemberSteve() {
    return new FamilyMember("Steve", ...)
}

但老实说,我也不推荐这个。正如我在评论中所说,使用 XML 或其他格式会更好,因为它会更灵活。

于 2018-05-21T10:35:12.310 回答