亲爱的,我想实现这个行为:
"入侵者将被枪杀,幸存者将再次被枪杀"
但是我得到了这个堆栈跟踪:
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)
Run Code Online (Sandbox Code Playgroud)
'物业'类:
package test;
public class Property {
private Shooter shooter = new Shooter();
public void punish(Person tresspasser) {
shooter.shoot(tresspasser);
}
}
Run Code Online (Sandbox Code Playgroud)
射手类:
package test;
public class Shooter {
public void shoot(Person person) {
if(!person.isDead()){
shoot(person);
}
}
}
Run Code Online (Sandbox Code Playgroud)
'人'类:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
最后,Main类:
package test;
public class Main {
public static void main(String args[]) {
Person person = new Person("Chuck Norris");
Property myProperty = new Property();
person.tresspass(myProperty);
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我使用eclipse,问题出现在Java 6,7和8 ......
S.
Zir*_*con 10
return !name.equals("Chuck Norris"); 如果这个人的名字是"Chuck Norris",那么总是返回false,因此你无限循环.
你可能有限的弹药,所以你应该考虑某种弹药功能.