dimanche 25 mai 2014

Java - création de méthodes dynamiquement dans une boucle - Stack Overflow


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.




No you can't create Array of method in java


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



  1. Java - Creating an array of methods

  2. Array of function pointers in Java



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.



No you can't create Array of method in java


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



  1. Java - Creating an array of methods

  2. Array of function pointers in Java


0 commentaires:

Enregistrer un commentaire