No enclosing instance of type E is accessible. Must qualify the allocation with an enclosin的解决方案

       最近用java写一个小程序时编译出现:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type  E(e.g.  x.new A() where x is an instance of E). 原始代码如下:

public  class WorkThreadTest {
	   class Worker {
		private String name; // 名字
		private long workDuration; // 工作持续时间

		/**
		 * 构造器
		 */
		public Worker(String name, long workDuration) {
			this.name = name;
			this.workDuration = workDuration;
		}

		/**
		 * 完成工作
		 */
		public void doWork() {
			System.out.println(name + " begins to work...");
			try {
				Thread.sleep(workDuration); // 用休眠模拟工作执行的时间
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			System.out.println(name + " has finished the job...");
		}
	}

	/**
	 * 测试线程
	 *
	 */
	     class WorkerTestThread implements Runnable {
		private Worker worker;
		private CountDownLatch cdLatch;

		public WorkerTestThread(Worker worker, CountDownLatch cdLatch) {
			this.worker = worker;
			this.cdLatch = cdLatch;
		}

		@Override
		public void run() {
			worker.doWork(); // 让工人开始工作
			cdLatch.countDown(); // 工作完成后倒计时次数减1
		}
	}

	private static final int MAX_WORK_DURATION = 5000; // 最大工作时间
	private static final int MIN_WORK_DURATION = 1000; // 最小工作时间

	// 产生随机的工作时间
	private static long getRandomWorkDuration(long min, long max) {
		return (long) (Math.random() * (max - min) + min);
	}

       public static void main(String[] args) {
    	       CountDownLatch latch = new CountDownLatch(2); // 创建倒计时闩并指定倒计时次数为2
		Worker w1 = new Worker("work1", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));
		Worker w2 = new Worker("work2", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));

		new Thread(new WorkerTestThread(w1, latch)).start();
		new Thread(new WorkerTestThread(w2, latch)).start();

		try {
			latch.await(); // 等待倒计时闩减到0
			System.out.println("All jobs have been finished!");
		} catch (InterruptedException e) {
			e.printStackTrace();
	}
    }
}


        根据提示,没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例。)于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。解决方案有两个:


方案一:先实例化一个WorkThreadTest,然后实例化内部类时将new Worker()改为 WorkThreadTest.new Worker().代码如下(修改在58-64行)

public  class WorkThreadTest {
	   class Worker {
		private String name; // 名字
		private long workDuration; // 工作持续时间

		/**
		 * 构造器
		 */
		public Worker(String name, long workDuration) {
			this.name = name;
			this.workDuration = workDuration;
		}

		/**
		 * 完成工作
		 */
		public void doWork() {
			System.out.println(name + " begins to work...");
			try {
				Thread.sleep(workDuration); // 用休眠模拟工作执行的时间
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			System.out.println(name + " has finished the job...");
		}
	}

	/**
	 * 测试线程
	 * 
	 *
	 */
	     class WorkerTestThread implements Runnable {
		private Worker worker;
		private CountDownLatch cdLatch;

		public WorkerTestThread(Worker worker, CountDownLatch cdLatch) {
			this.worker = worker;
			this.cdLatch = cdLatch;
		}

		@Override
		public void run() {
			worker.doWork(); // 让工人开始工作
			cdLatch.countDown(); // 工作完成后倒计时次数减1
		}
	}

	private static final int MAX_WORK_DURATION = 5000; // 最大工作时间
	private static final int MIN_WORK_DURATION = 1000; // 最小工作时间
	// 产生随机的工作时间
	private static long getRandomWorkDuration(long min, long max) {
		return (long) (Math.random() * (max - min) + min);
	}
    
       public static void main(String[] args) {
    	     CountDownLatch latch = new CountDownLatch(2); // 创建倒计时闩并指定倒计时次数为2
    	     WorkThreadTest workThreadTest=new WorkThreadTest();
	     Worker w1 = workThreadTest.new Worker("work1", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));
	     Worker w2 = workThreadTest.new Worker("work2", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));

	    new Thread(workThreadTest.new WorkerTestThread(w1, latch)).start();
	    new Thread(workThreadTest.new WorkerTestThread(w2, latch)).start();

	try {
		latch.await(); // 等待倒计时闩减到0
		System.out.println("All jobs have been finished!");
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
    }
}

方案二:将class改为static class(修改在第2、33行)

 

public  class WorkThreadTest {
	   static class Worker {
		private String name; // 名字
		private long workDuration; // 工作持续时间

		/**
		 * 构造器
		 */
		public Worker(String name, long workDuration) {
			this.name = name;
			this.workDuration = workDuration;
		}

		/**
		 * 完成工作
		 */
		public void doWork() {
			System.out.println(name + " begins to work...");
			try {
				Thread.sleep(workDuration); // 用休眠模拟工作执行的时间
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
			System.out.println(name + " has finished the job...");
		}
	}

	/**
	 * 测试线程
	 * 
	 *
	 */
	    static class WorkerTestThread implements Runnable {
		private Worker worker;
		private CountDownLatch cdLatch;

		public WorkerTestThread(Worker worker, CountDownLatch cdLatch) {
			this.worker = worker;
			this.cdLatch = cdLatch;
		}

		@Override
		public void run() {
			worker.doWork(); // 让工人开始工作
			cdLatch.countDown(); // 工作完成后倒计时次数减1
		}
	}

	private static final int MAX_WORK_DURATION = 5000; // 最大工作时间
	private static final int MIN_WORK_DURATION = 1000; // 最小工作时间

	// 产生随机的工作时间
	private static long getRandomWorkDuration(long min, long max) {
		return (long) (Math.random() * (max - min) + min);
	}

       public static void main(String[] args) {
    	       CountDownLatch latch = new CountDownLatch(2); // 创建倒计时闩并指定倒计时次数为2
		Worker w1 = new Worker("work1", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));
		Worker w2 = new Worker("work2", getRandomWorkDuration(MIN_WORK_DURATION,MAX_WORK_DURATION));

		new Thread(new WorkerTestThread(w1, latch)).start();
		new Thread(new WorkerTestThread(w2, latch)).start();

		try {
			latch.await(); // 等待倒计时闩减到0
			System.out.println("All jobs have been finished!");
		} catch (InterruptedException e) {
			e.printStackTrace();
	}
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值