成员方法的反射

来源:互联网 发布:linux mysql 启动失败 编辑:程序博客网 时间:2024/05/21 22:56

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

 

public class ReflectTest {
 public static void main(String[] args) throws Exception {
  String str1="abc";
  
  Method methodCharAt=String.class.getMethod("charAt", int.class);
  System.out.println(methodCharAt.invoke(str1,2));
  /*invoke 是方法对象methodCharAt上的方法,这个方法的作用是去调用
   * methodCharAt对应的方法*/
 }
 }
 

 

原创粉丝点击