求大神解答

南橘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).

请问要怎么才能解决呀!
求大神指导
...全文
93 点赞 收藏 4
写回复
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);
回复
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2018-12-05 02:11
社区公告
暂无公告