Java中的硬编码值

时间:2018-05-21 10:19:11

标签: java testing defaultmutabletreenode

目前我正在尝试使用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" )

3 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分: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或其他格式,因为它会更灵活。

答案 2 :(得分: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;
}