No enclosing instance of type A is accessible. Must qualify t找不到哪里错了。。。
问题描述:
package com.a.thread.thread4;
public class A
{
class Inner
{
public synchronized void a(int time) throws InterruptedException
{
Thread.sleep(time);
System.out.println("a: " + time);
}
public synchronized void b(int time) throws InterruptedException
{
Thread.sleep(time);
System.out.println("b: " + time);
}
}
class MyThread extends Thread
{
private Inner i;
private int time;
public MyThread(Inner i, int time)
{
this.i = i;
this.time = time;
}
public void run()
{
try
{
i.a(time);
i.b(time);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
Inner i = new A().new Inner();
MyThread m = new MyThread(i, 3000);
new MyThread(i, 3000).start();
new MyThread(i, 1000).start();
}
}
在MyThread m = new MyThread(i, 3000);
new MyThread(i, 3000).start();
new MyThread(i, 1000).start();
都错了。。。
y?
答
MyThread是内部类,实例化的时候要先实例化外部类。改成这样:
[code="java"]Inner i = new A().new Inner();
A a=new A();
MyThread m = a.new MyThread(i, 3000);
a.new MyThread(i, 3000).start();
a.new MyThread(i, 1000).start();[/code]