我是一名正在学习基本面向对象java的学生,我正在尝试创建一个OOP程序,其中有3个类YouthGroup,Section和成员一个Youthgroup有3个部分,这个程序的功能应该是一个成员不能列出在同一部分两次,我正在尝试创建此功能:
public boolean setMembers(members members) {
boolean crouded = true;
for(int i = 0; i<this.getMembers().size();i++) {
if(!this.members.contains(member)) {
this.members.add(member);
}
else {
System.out.println("Member already exists in this section");
crouded = false;
}
}
return crouded;
}
现在在主要方法中,我正在做以下事情:
public static void main(String[] args) {
List<member> m = new ArrayList<>();
Section s = new Section(m,"programming",3);
member m1 = new member("tom",21,"male",s);
member m2 = new member("kate",20,"Female",s);
m.add(m1);
m.add(m2);
s.setMembers(m1);
s.filterAge(21);
System.out.println(s);
}
它给了我以下错误:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:648)
at java.lang.StringBuilder.append(StringBuilder.java:208)
at youthgroup.member.toString(member.java:62)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.member.toString(member.java:64)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at youthgroup.Section.toString(Section.java:68)
at java.lang.String.valueOf(String.java:2981)
at java.lang.StringBuilder.append(StringBuilder.java:131)
它会永远持续下去。
这是会员类:
package youthgroup;
import java.util.Objects;
/**
*
* @author student
*/
public class member {
private String name;
private int age ;
private String gender;
private Section section;
public member(String name, int age, String gender, Section section) {
this.name = name;
this.age = age;
this.gender = gender;
this.section = section;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Section getSection() {
return section;
}
public void setSection(Section section) {
this.section = section;
}
@Override
public String toString() {
String word = "Member name " +name;
word+= "age : "+getAge();
word+="Gender : "+ getGender();
word+= " is in section : "+getSection();
return word;
}
@Override
public int hashCode() {
int hash = 5;
hash = 67 * hash + this.age;
hash = 67 * hash + Objects.hashCode(this.gender);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final member other = (member) obj;
if (this.age != other.age) {
return false;
}
if (!Objects.equals(this.gender, other.gender)) {
return false;
}
return true;
}
}
这是部分类:
public class Section {
private List<member>members;
private String sectionName;
private int maxNumber;
public Section(List<member> members, String sectionName, int maxNumber) {
this.members = members;
this.sectionName = sectionName;
this.maxNumber = maxNumber;
}
public List<member> getMembers() {
return members;
}
public boolean setMembers(member member) {
boolean crouded = true;
for(int i = 0; i<this.getMembers().size();i++){
if(!this.members.contains(member)){
this.members.add(member);
}else{
System.out.println("Member already exists in this section");
crouded = false;
}
}
return crouded;
}
public String getSectionName() {
return sectionName;
}
public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}
public int getMaxNumber() {
return maxNumber;
}
public void setMaxNumber(int maxNumber) {
this.maxNumber = maxNumber;
}
@Override
public String toString() {
String word = " section name is : "+ getSectionName();
word+=" Max number of members it can contain : "+ getMaxNumber();
word+= " List of members : "+getMembers();
return word;
}
public boolean filterByAge(int a){
boolean found = true;
for(int i = 0; i<this.members.size(); i++){
if(this.members.contains(a)){
System.out.println("These members where found with this age : "+ this.members.get(i).getName());
}else{
System.out.println("No member found !!!");
found = false;
}
}
return found;
}
我真的很感激任何帮助,谢谢你。
答案 0 :(得分:2)
您的toString()
方法正在对自己进行递归调用。
toString()
方法调用getSection()
将返回Section
对象,该对象将toString()
显式调用它。这个toString()
本身会获得List
member
个toString()
个对象,它会调用toString()
,但是,第二个member
方法本身会回调toString()
{1}} StackOverflowError
方法,将再次回拨,再次回拨。我想,你看到了问题。
这将导致word+= " is in section : "+getSection();
//section is returned and called toString()
word+= " List of members : "+getMembers();
//List of members are returned and called `toString()`, which will loop back to first line I've written
,因为它将无限循环到调用堆栈中。
+
这里要理解的重要一点是在Java中使用Objects
运算符时会发生什么。在String
,包括Objects
个对象或您定义的任何+
之间使用时,Strings
运算符会将其左侧和右侧参数显式转换为toString()
。在Java中完成此操作的方式是对您已覆盖的Object
方法的隐式调用,但如果您没有覆盖它,它仍然存在(它位于{{1}}超类中,所以全部Java对象继承了一些基础实现)