ディアーズ、私はこの動作を実装する:簡単なプログラムでは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);
}
}
?人の名前が「チャック・ノリス」であるため、あなたループ無限にあれば
私は日食使用して、問題は、Java 6、図7、図8で発生...
S.は
s/Chuck Norris/Jon Skeet/ – Mureinik
これは、デバッガでコードをステップ実行することで、なぜそれが永久に再発するのかを理解するのに役立ちます。 –