1,静态方法反射
[java] view plaincopy
- import java.lang.reflect.Method;
-
- public class MethodDemo {
- public static void main(String[] args)throws Exception {
- /**
- * 如果invoke(null,参数),那么就表示调用此方法的方法是静态方法
- */
-
- Method methosMax=Math.class.getMethod("max", int.class,int.class);
- System.out.println(methosMax.invoke(null, 23, 57));
- }
-
- }
2、主函数(main)反射
[java] view plaincopy
- import java.lang.reflect.Method;
-
- public class mainDemo {
- public static void main(String[] args) throws Exception {
-
- Method mianMethod = Class.forName("wang.fuxi.jiaqiang.text").getMethod(
- "main", String[].class);
- mianMethod.invoke(null, new Object[] { new String[] { "123", "456",
- "789" } });
- }
-
- }
-
- class text {
- public static void main(String[] agrs) {
- System.out.println(agrs.length);
- }
- }
- 结果:
- 3
|
|