| Page 16 of 38 Prev | Index | Next |
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
public class ExReflection {
public static void main(String[] args) {
List a = new ArrayList();
a.add(new Integer(1));
Method g = null;
try {
g = a.getClass().getMethod("get", new Class[] {Integer.TYPE});
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
System.out.println(g);
try {
System.out.println(g.invoke(a, new Object[] {new Integer(0)}));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
from java.util import ArrayList
a = ArrayList()
a.add(1)
g = getattr(a, "get")
print g
print g(0)
# alternatively
g = a.get
| Page 16 of 38 Prev | Index | Next |