Java:我的错误是在线程的运行方法中找不到符号?

问题描述 投票:0回答:1
public class Battle extends Thread{

   public Battle(Object instructor1, Object instructor2){

}

public void run(){

  while ((instructor1.getCurrentHP() > 0) || (instructor2.getCurrentHP() > 0)){ //ERROR HERE
     System.out.print(2);
  }

}

public static void main(String[] args){
   Instructor instructor1 = new Instructor("Big Omar Latif", 999, 145, 180, 4000);
   Instructor instructor2 = new Instructor("Small Ali Raza", 400, 185, 230, 1200);
   Battle x = new Battle(instructor1, instructor2);

   x.start();

} 


} 

'''所以这是我的代码。我有2位教官互相搏斗,他们将进行多轮比赛。我想要一场战斗。现在,当我运行此命令时,对于教员1和教员2的while循环,出现未找到符号错误。我假设我对run()方法的理解尚不清楚。您能帮我解决吗?

java multithreading
1个回答
2
投票

您必须将对象讲师1,对象讲师2声明为全局变量,并在构造函数中对其进行初始化,以便它们可以在不同的方法上进行访问。

  public class Battle extends Thread{

    private Object instructor1;
    private Object instructor2;

      public Battle(Object instructor1, Object instructor2){
         this.instructor1=instructor1;
         this.instructor2=instructor2;
      }
}
© www.soinside.com 2019 - 2024. All rights reserved.