反射的功能:
对于一个任意给定的类,都能获得它的构造方法,字段和相应的方法。
常用方法:
- 加载类:forName()
- 反射的常用方法:
-getConstructor(Class <>… Parameter Types)
获取公有构造函数
-getMethod(String name, Class <>… Parameter Types)
获取公有方法
-getField(String name) public
获取公有的字段 - 私有的:
-getDeclaredConstructor(Class <>… Parameter Types)
-getDeclaredMethod(String name, Class <>… Parameter Types)
-getDeclaredField(String name)
应用实例:
构造函数:
@Test
public void test1() throws Exception{
Class clazz = Class.forName("reflect.Person");
//加载类
Constructor c = clazz.getConstructor(null);
//获得一个构造函数的对象
Person p = (Person) c.newInstance(null);
//通过c中的newInstance()方法,获得person实例
// System.out.println(p.name);
}
@Test
public void test2() throws Exception{
Class clazz = Class.forName("reflect.Person");
//加载类
Constructor c = clazz.getConstructor(String.class);
//获得一个构造函数的对象
Person p = (Person) c.newInstance("lallal");
//通过c中的newInstance()方法,获得person实例
System.out.println(p.name);
}
@Test
public void test3() throws Exception{
Class clazz = Class.forName("reflect.Person");
//加载类
Constructor c = clazz.getDeclaredConstructor(List.class);
//获得一个构造函数的对象
c.setAccessible(true);
//暴力反射
Person p = (Person) c.newInstance(new ArrayList());
//通过c中的newInstance()方法,获得person实例
System.out.println(p.name);
}
方法:
//public void aa1()
@Test
public void test1() throws Exception{
Person p = new Person();//有一个Person类,创建一个Person的实例对象
Class clazz = Class.forName("reflect.Person");//加载类
Method method = clazz.getMethod("aa1", null);//获取反射方法
method.invoke(p, null);//执行方法
}
//public void aa1(String name)
@Test
public void test2() throws Exception{
Person p = new Person();
Class clazz = Class.forName("reflect.Person");
Method method = clazz.getMethod("aa1", String.class);
method.invoke(p, "nihao");
}
// public Class[] aa1(String name, int[] password)
@Test
public void test3() throws Exception{
Person p = new Person();
Class clazz = Class.forName("reflect.Person");
Method method = clazz.getMethod("aa1", String.class, int[].class);
Class cs[] = (Class[]) method.invoke(p, "nihao", new int[]{1,2,3});
System.out.println(cs);
}
//private void aa1(InputStream in)私有成员函数
@Test
public void test4() throws Exception{
Person p = new Person();
Class clazz = Class.forName("reflect.Person");
Method method = clazz.getDeclaredMethod("aa1", InputStream.class);
method.setAccessible(true); //强暴一把,获得访问权限
method.invoke(p, new FileInputStream("D:\\1.txt"));
}
//public static void aa1(int i)静态函数
@Test
public void test5() throws Exception{//不需要创建实例
Class clazz = Class.forName("reflect.Person");
Method method = clazz.getDeclaredMethod("aa1", int.class);
method.invoke(null, 1);
}
字段:
//public String name = "nihaoa";
@Test
public void test1() throws Exception{
Person p = new Person();
Class clazz = Class.forName("reflect.Person");
Field f = clazz.getField("name");
//获字段的取值
String value = (String) f.get(p);
System.out.println(value);
//获取字段的数据类型
Class type = f.getClass();
System.out.println(type);
//更改字段的值
f.set(p, "aaaaa");
System.out.println((String)f.get(p));
}
//private String name1 = "wohao";
@Test
public void test2() throws Exception{
Person p = new Person();
Class clazz = Class.forName("reflect.Person");
Field f = clazz.getDeclaredField("name1");
f.setAccessible(true);
//获字段的取值
String value = (String) f.get(p);
System.out.println(value);
//获取字段的数据类型
Class type = f.getClass();
System.out.println(type);
//更改字段的值
f.set(p, "aaa");
System.out.println(f.get(p));
}
//public static String name2 = "dajihao";
//同public
映射一般应用在框架中