为什么我越来越*Error

问题描述:

public class Category { 

    private Category parentCategory; 
    private Set<Category> childCategories; 
    private String name; 

    public Category() { 
     childCategories = new HashSet<Category>(); 
    } 

    public Category getParentCategory() { 
     return parentCategory; 
    } 

    public void setParentCategory(Category parentCategory) { 
     this.parentCategory = parentCategory; 
    } 

    public Set<Category> getChildCategories() { 
     return childCategories; 
    } 

    public void setChildCategories(Set<Category> childCategories) { 
     this.childCategories = childCategories; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return "Category [childCategories=" + childCategories + ", name=" 
       + name + ", parentCategory=" + parentCategory + "]"; 
    } 

} 


public static void main(String[] args) { 
     Category books = new Category(); 
     books.setName("Books"); 
     books.setParentCategory(null); 

     Category novels = new Category(); 
     novels.setName("Novels"); 
     novels.setParentCategory(books); 

     books.getChildCategories().add(novels); 
     //novels.setChildCategories(null); 

     System.out.println("Books > " + books); 
    } 

System.out.println正在生成*Error为什么我越来越*Error

当你做你的toString(),你打电话toString()的孩子。这里没有问题,除了你在这里打电话给父母的toString()。这会叫toString()的孩子等

好无限循环。

最好的办法摆脱它是改变你的toString()方法为:

@Override 
public String toString() { 
    return "Category [childCategories=" + childCategories + ", name=" 
      + name + ", parentCategory=" + parentCategory.getName() + "]"; 
} 

这样,您就不能打印parentCategory而只是它的名字,没有无限循环,没有的*Error。

编辑:正如下面的博洛说,你需要检查parentCategory不是null,如果是的话你可能有NullPointerException


资源:

关于同一主题:

+0

你会得到'NullPointerException'而不是当'parentCategory == null'。 – Bolo 2010-08-29 07:36:24

+0

只是'parentCategory.getName()'不会完整。 – fastcodejava 2010-08-29 07:57:35

因为每次调用Category#toString()产生一吨其他toString S的。请注意,打印childCategories会导致打印出每个元素(通过toString),这又会重复执行许多toString方法调用的整个过程。

不仅如此,但每个孩子在其母公司,后者又调用它的孩子,每个父,进而在其子女要求toString,它调用toStringtoString调用toString ...

return "Category [childCategories=" + childCategories + ", name=" 
       + name + ", parentCategory=" + parentCategory + "]"; 

您正在使用像parentCategory这样的实例来连接到您的toString。它会调用这些实例的toString方法。

toString调用的这个循环永远不会结束。因为孩子类别将调用父类和家长会一次这么叫蔡尔兹...

没有,如果你把:System.out.println(myObject);它实际上是:System.out.println(myObject.toString());

既然错误是System.out.println问题必须在toString()

问题是toString()会打印使用toString()方法打印的父对象和子对象Category。所以当你打印一个类别时,它调用父母上的toString(),该父母呼叫toString()对孩子调用toString()对父母调用toString()对孩子等,直到堆栈用尽。

您的toString()正在进入递归尾巴。你需要有两个toString();一个为父母,一个为孩子。您可以toString是这样的:

@Override 
public String toString() { 
    toStringParent(parent); // This print only parent 
    toStringChildren(children); // This print only children 
} 

*Error在构建String说法,将被传递给System.out.println实际上提高。

每次连接StringCategory时,都会执行方法Category。问题是你的toString()Category有点太冗长。要解决这个问题的方法之一是打印的父类的唯一名称(跳过它的父和儿童):

@Override 
public String toString() { 
    return "Category [childCategories=" + childCategories + ", name=" 
      + name + ", parentCategory=" 
      + ((parentCategory == null) ? null : parentCategory.getName()) 
      + "]"; 
    }