java - Construct the name of the method to be called at runtime -


i'm learning java , i'm new this. here problem pseudocode:

public void objectcaller(int objectnumber) {     switch(objectnumber) {       case 1:      object1.setfill(color.red);      break:       case 2:      object2.setfill(color.red);      break;      .      .and on      } } 

is there way replace in way that?

public void objectcaller(int objectnumber) {     (object + objectnumber).setfill(color.red);  } 

it not concrete problem. thinking if possible assemble object names.

you reflection, case overkill. approach?: store objects in array (or list) , use index access required one. note i'm assuming objects of same class or least have parent-child class relationship, @chetankinger pointed in comment below.

public void methodcaller(int methodnumber) {    myarrayofobjects[methodnumber].setfill(color.red); } 

ps: in fact, trying compose "object" names, not methods names. in case need use reflection api


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -