求大神解答

南橘ryc 2018-12-05 02:11:01
package bookdemov1;

public class VectorDemo {

public static void main(String[] args) throws InterruptedException {
Boss boss = new Boss(100);!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*****************8
AttackThread[] threads = new AttackThread[10];
threads[1].start();
}

class Boss {
public int hp;

public Boss(int hp) {
this.hp = hp;
}

public synchronized void hited() {

this.hp -= 6;
}
}

class AttackThread extends Thread {
private Boss boss;

public AttackThread(Boss boss) {
this.boss = boss;
}

public void run() {

for (int i = 0; i < 10; i++) {
boss.hited();
}

}
}

}

打!!!!!!!!***********号那一栏出现了问题

No enclosing instance of type VectorDemo is accessible. Must qualify the allocation with an enclosing instance of type VectorDemo (e.g. x.new A() where x is an instance of VectorDemo).

请问要怎么才能解决呀!
求大神指导
...全文
121 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
南橘ryc 2018-12-06
  • 打赏
  • 举报
回复
引用 1 楼 nayi_224 的回复:
改成这样
Boss boss = new VectorDemo().new Boss(100);


改完以后出现这个问题 大神
Exception in thread "main" java.lang.NullPointerException
at Book/bookdemov1.VectorDemo.main(VectorDemo.java:12)
南橘ryc 2018-12-06
  • 打赏
  • 举报
回复
引用 3 楼 nayi_224 的回复:
public static void main(String[] args) throws InterruptedException {
Boss boss = new VectorDemo().new Boss(100);
AttackThread[] threads = new AttackThread[10];
for(int i = 0; i < threads.length; i++){
threads[i] = new VectorDemo().new AttackThread(boss);
}
threads[1].start();
}


谢谢大神!!
orz
nayi_224 2018-12-06
  • 打赏
  • 举报
回复
public static void main(String[] args) throws InterruptedException { Boss boss = new VectorDemo().new Boss(100); AttackThread[] threads = new AttackThread[10]; for(int i = 0; i < threads.length; i++){ threads[i] = new VectorDemo().new AttackThread(boss); } threads[1].start(); }
nayi_224 2018-12-05
  • 打赏
  • 举报
回复
改成这样 Boss boss = new VectorDemo().new Boss(100);

23,404

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧