re一个匿名内部类的多个实例 [英] re More than one instance of an anonymous inner class

查看:287
本文介绍了re一个匿名内部类的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我对此主题中提供的问题的回答有关:是内心的类轻量级?

This is in relation to my answer to a question provided in this thread: Are Inner Classes lightweight?

我记得从我的阅读中可以看出,如果你只能从一个匿名的内部类创建一个对象,那么,如果你想要的话要创建一个 ActionListener 类,并希望从这一个类(不使用反射)创建多个对象,不要使用匿名内部类,而是使用私有内部类或一个阶级的立场,但人们告诉我,我错了。有人可以帮我澄清一下吗?请检查链接,因为它包含更多详细信息,但如果有任何不清楚的地方,请随时询问!

I remember from my reading that if you can only create one object from a single anonymous inner class, and for this reason, if you want to say create an ActionListener class and want to create multiple objects from this one class (not using reflection), to not use an anonymous inner class but rather either a private inner class or a stand along class, but folks are telling me I'm wrong. Can someone please clarify this for me? Please check the link as it contains more details, but if anything is unclear, please ask away!

推荐答案

您可以创建任何数字对于匿名类对象,您可以在代码中的一个位置创建它们(除非您复制代码)

You can create any number of anonymous class objects, you can create them in one place in your code (unless you copy the code)

ExecutorService service = ...
for(int i=0;i<1000*1000;i++) {
   final int finalI = i;
   service.submit(new Runnable() {
      public void run() {
         System.out.println("Task "+finalI+" run.");
      }
   });
}

此代码将创建100万个同类的对象。

This code will create 1 million objects of the same class.

这篇关于re一个匿名内部类的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆