 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sergio Guest
|
Posted: Fri Sep 26, 2003 8:26 am Post subject: passaggio di nome metodo come parametro. |
|
|
E' possibile passare il nome del metodo da chiamare come parametro?
Faccio un esempio
ClasseA classeA = new ClasseA(this);
classeA.gestisci(pippo);
Dentro ClasseA
public void gestisci(metodo){
....
....
parent.metodo
}
Grazie
Sergio
--------------------------------
Inviato via http://usenet.libero.it
|
|
| Back to top |
|
 |
Zio Pino Guest
|
Posted: Fri Sep 26, 2003 8:57 am Post subject: Re: passaggio di nome metodo come parametro. |
|
|
Sergio wrote:
| Quote: | E' possibile passare il nome del metodo da chiamare come parametro?
Faccio un esempio
|
Dai un'occhiata:
package test;
import java.lang.reflect.Method;
/*
* Created on 26-set-2003
*/
/**
* @author dimitri
*/
public class ReflectionTest {
public void metodo_1() {
System.out.println("invocazione di ReflectionTest.metodo_1()");
}
public void metodo_2(String parametro) {
System.out.println("invocazione di ReflectionTest.metodo_2(" +
parametro + ")");
}
/* Esempi di invocazione */
// invoca un metodo dichiarato senza parametri come metodo_1()
public void invocaMetodo_1() throws Exception {
Class[] parameterTypes = new Class[0];
Method method = this.getClass().getDeclaredMethod("metodo_1",
parameterTypes);
// invoca senza parametri
Object[] params = new Object[0];
method.invoke(this, params);
}
// invoca un metodo dichiarato con parametri come metodo_2(String)
public void invocaMetodo_2() throws Exception {
Class[] parameterTypes = new Class[1];
parameterTypes[0] = String.class;
Method method = this.getClass().getDeclaredMethod("metodo_2",
parameterTypes);
// invoca con parametri
Object[] params = new Object[1];
params[0] = "Parametro di prova";
method.invoke(this, params);
}
public static void main(String[] args) {
try {
ReflectionTest rt = new ReflectionTest();
rt.invocaMetodo_1();
rt.invocaMetodo_2();
} catch (Exception e) {
System.err.println("Eccezione: " + e.getMessage());
}
}
}
....però mi raccomando dai un'occhiata al Java Tutorial della Sun e
soprattutto i javadoc del package java.lang.reflect !
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|