Outils pour utilisateurs

Outils du site


invoquer_une_methode_par_son_nom

Invoquer une méthode par son nom

Cet exemple permet d'appeler une méthode dynamiquement avec son nom. Il existe la méthode mehod() de Processing, mais celle-ci ne permet pas de passer des paramètres.

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
void setup() {
  noLoop();
}
 
void draw() {
  invoquer("maMethode", " C'est super !");
}
 
void invoquer(String s, String argument) { 
  try {
    Method m = this.getClass().getMethod(s, String.class);
    m.invoke(this, argument);
  } 
  catch (NoSuchMethodException x) {
    x.printStackTrace();
  }
  catch (InvocationTargetException x) {
    x.printStackTrace();
  }
  catch (IllegalAccessException x) {
    x.printStackTrace();
  }
}
 
void maMethode(String s) {
  println("Je suis la methode invoquée." + s);
}
invoquer_une_methode_par_son_nom.txt · Dernière modification : 2020/04/25 02:06 de Mushussu