gpt4 book ai didi

java - OOP 解析并将对象添加到列表中

转载 作者:行者123 更新时间:2023-11-30 07:32:26 25 4
gpt4 key购买 nike

因此,我有三个类,其中 Class1 用于不同的对象。在主类中,创建 Class1 对象并设置名称。主类还创建新的 Household 对象,为家庭指定名称,最后将人员添加到家庭中,如下所示:

HouseholdOne.addPeopleToHousehold(person1);

当前问题不涉及主类。

public class Class1 {
private String firstName;
private String middleName;
private String lastName;

public String setFirstName(String firstName) {
return this.firstName = firstName;
}
public String setLastName(String lastName) {
return this.lastName = lastName;
}
public String setMiddleName(String middleName) {
return this.middleName = middleName;
}
public String toString() {
if(firstName == "" && middleName == "" && lastName == "") {
return "";
} else if (firstName == null && middleName == null && lastName == null) {
return null;
} else
return firstName + " \"" + middleName + "\" " + lastName;
}
}

在第二类 Household“addPeopleToHousehold”方法中,应从参数中解析出人员的中间名并将其添加到人员列表中。

我想使用.split函数,但它不适用于对象参数。我怎样才能得到给定的人的中间名和

peopleList.add(personMiddleName)

同样在 Household 类中,toString 应该打印出家庭成员,例如:

成员1、成员2、...、成员x

public class Household {

List<Class1> peopleList = new ArrayList<>();
public String householdName;

public String setHouseholdName(String householdName) {
return this.householdName = householdName;
}

public void addPeopleToHousehold(Class1 people) {
// implementation needed //
}

public int getNumberOfPeople() {
return people.size();
}

public String toString() {
if (householdName == null || householdName == "") {
return "" + people;
}
return householdName + ": " + people;
}
}

最佳答案

也许是这样的?很难破译你的帖子,但各种方法的签名说明了很多。我的更改只是添加 people 到 ArrayList peopleList 中。当您打印“家庭”时,它会首先检查是否存在有效的家庭姓名,然后循环“家庭”中的每个人并将其全名添加到列表中。 ((i + 1) != peopleList.size()) 仅用于用 , 分隔姓名,姓氏除外。

更新了Class1。修复了 setter 并添加了中间名的公共(public) getter。

public class Class1 {

private String firstName;
private String middleName;
private String lastName;

/*
* Setters
*/

public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}

/*
* Getters
*/

public String getMiddleName() {
return middleName;
}

/*
* toString
*/

public String toString() {
if(firstName == "" && middleName == "" && lastName == "") {
return "";
} else if (firstName == null && middleName == null && lastName == null) {
return null;
} else
return firstName + " \"" + middleName + "\" " + lastName;
}
}

更新了Household类。各种修复:

public class Household {

List<Class1> peopleList = new ArrayList<>();
public String householdName;

public String setHouseholdName(String householdName) {
this.householdName = householdName;
}

public void addPeopleToHousehold(Class1 people) {
peopleList.add(people);
}

public int getNumberOfPeople() {
return peopleList.size();
}

public String toString() {
String returnString = "";
if (householdName != null) {
returnString = householdName + ": ";
}

// Loop the members
for (int i = 0; i < peopleList.size(); i++) {
returnString += peopleList.get(i).getMiddleName();
if ((i + 1) != peopleList.size()) {
returnString += ", ";
}
}

return returnString;
}
}

关于java - OOP 解析并将对象添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35900547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com