逗人,我要實現此行爲:的StackOverflowError在簡單的程序
「侵入者就會出手,倖存者將被再次出手」
但我得到這個堆棧跟蹤:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.equals(String.java:975)
at test.Person.isDead(Person.java:14)
at test.Shooter.shoot(Shooter.java:7)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
at test.Shooter.shoot(Shooter.java:8)
的'財產' 類:
package test;
public class Property {
private Shooter shooter = new Shooter();
public void punish(Person tresspasser) {
shooter.shoot(tresspasser);
}
}
的射擊類:
package test;
public class Shooter {
public void shoot(Person person) {
if(!person.isDead()){
shoot(person);
}
}
}
的 '人' 類:
package test;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void tresspass(Property property) {
property.punish(this);
}
public boolean isDead(){
return !name.equals("Chuck Norris");
}
}
最後,主類:
package test;
public class Main {
public static void main(String args[]) {
Person person = new Person("Chuck Norris");
Property myProperty = new Property();
person.tresspass(myProperty);
}
}
我在做什麼錯?如果這個人的名字是「查克·諾里斯」,因此你無限循環
我使用Eclipse和Java 6中,7和8出現問題...
S.
s /查克諾里斯/喬恩Skeet/ – Mureinik
這是在調試器中單步執行代碼將幫助你明白爲什麼它永遠遞歸。 –