在循环中动态创建方法

时间:2022-11-29 20:51:44

I want to do something like this:

我想做这样的事情:

for(int i = 0; i<5;i++)
    {
        public void test[i]()
        {
            System.out.println(i);
        }   
    }

Can I even create methods like this? I know it requires an array but I have read everywhere about method arrays and it doesn't do it like I want it to.

我甚至可以创建这样的方法吗?我知道它需要一个数组,但我已经阅读了各种关于方法数组的内容,而且它并没有像我想要的那样去做。

I don't want to make several methods and then save them in an array, I want to create methods in a loop, and I don't know how to do this.

我不想制作几个方法,然后将它们保存在一个数组中,我想在循环中创建方法,我不知道如何做到这一点。

I need it so I can make as many methods I want of this certain method, instead of writing 100 methods.

我需要它,所以我可以制作我想要的这种方法,而不是编写100种方法。

1 个解决方案

#1


0  

No you can't create Array of method in java

不,你不能在java中创建方法数组

But This sort of thing tends to be done with anonymous subclasses.

但是这种事情往往是用匿名子类完成的。

  1. Java - Creating an array of methods
  2. Java - 创建方法数组

  3. Array of function pointers in Java
  4. Java中的函数指针数组

#1


0  

No you can't create Array of method in java

不,你不能在java中创建方法数组

But This sort of thing tends to be done with anonymous subclasses.

但是这种事情往往是用匿名子类完成的。

  1. Java - Creating an array of methods
  2. Java - 创建方法数组

  3. Array of function pointers in Java
  4. Java中的函数指针数组