- package heima.demo;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- public class ReflexDemo {
- public static void main(String[] args) throws Exception {
- List<String> list = new ArrayList<String>();
-
- Method method = list.getClass().getMethod("add", Object.class);
-
- method.invoke(list, 1);
-
- System.out.println(list);
- }
- }
复制代码
通过List的字节码文件获取add方法,设置传入参数类型为Object类型就可以了 |