反射:
概念:反射就是加载类,并解剖出类的各个组成部分
加载类的三种方法(假如这个类是Person):
a-Class class1=Class.forName("my.reflect.Person");
//在my.reflect包下
b-Class class2=
new Person().getClass();
c-Class class3=Person.
class;
Class的几种方法:
--->
解剖public方法
public Constructor getConstructor(Class<?>
...parameterType);
public Method getMethod(String name,Class<?>
...parameterType);
public Field getField(String name)
--->
解剖私有的
public Constructor getDeclaredConstructor(Class<?>
...parameterType);
public Method getDeclaredMethod(String name,Class<?>
...parameterType);
public Field getDeclaredField(String name)
分别解剖出构造函数,方法和成员变量
案例:
Person类
public class Person {
public String name="aaa"
;
public Person(){
System.out.println("Person"
);
}
public Person(String name){
System.out.println("Person name"
);
}
public Person(String name,
int password){
System.out.println("Peron name-password"
);
}
private Person(ArrayList list){
System.out.println("list"
);
}
}
测试方法:
@Test
public void test2()
throws Exception{
Class clazz2=Class.forName("my.reflect.Person"
);
Constructor c=clazz2.getConstructor(String.
class);
Person p=(Person)c.newInstance("XXXX"
);
System.out.println(p.name);
}
//测试无参构造方法
@Test
public void test3()
throws Exception{
Class clazz=Class.forName("my.reflect.Person"
);
Constructor c=clazz.getConstructor(
null);
Person p=(Person)c.newInstance(
null);
System.out.println(p.name);
}
//测试参数为字符串,整数的构造方法
@Test
public void test4()
throws Exception{
Class clazz2=Class.forName("my.reflect.Person"
);
Constructor c=clazz2.getConstructor(String.
class,
int.
class);
Person p=(Person)c.newInstance("XXXX",10
);
System.out.println(p.name);
}
@Test
public void test5()
throws Exception{
Class clazz2=Class.forName("my.reflect.Person"
);
Constructor c=clazz2.getDeclaredConstructor(ArrayList.
class);
c.setAccessible(true);
//暴力反射
Person p=(Person)c.newInstance(
new ArrayList());
System.out.println(p.name);
}
Method:
1.4
:invoke(object,object[])
1.5:invoke(object,object...parameter):参数只用一个String[]的时候,对String[]会拆,->invoke(obj,(Object)
new String[]{...},
或者method.invoke(p,new Object[]{
new String[]{"1","2,3"
}});让它拆
//反射类的public void a1()
@Test
public void testMethod1()
throws Exception{
Person p=
new Person();
Class clazz=Class.forName("my.reflect.Person"
);
Method method=clazz.getMethod("a1",
null);
method.invoke(p, null);
}
//测试public void a2(String name,int password)
@Test
public void testMethod2()
throws Exception{
Person p=
new Person();
Class clazz=Class.forName("my.reflect.Person"
);
Method method=clazz.getMethod("a2",String.
class,
int.
class);
method.invoke(p,"luyi",123
);
}
//测试public void a3(int[] password)
@Test
public void testMethod3()
throws Exception{
Person p=
new Person();
Class clazz=Class.forName("my.reflect.Person"
);
Method method=clazz.getMethod("a3",
int[].
class);
method.invoke(p,new int[]{1,2,3
});
}
@Test //public static void a11(int num)
public void testMethod5()
throws Exception{
Class clazz=Class.forName("my.reflect.Person"
);
Method method=clazz.getMethod("a11",
int.
class);
method.invoke(null,5);
//静态方法调用的时候不需要对象也可以调用
}
获得字段:
----
方法:
Field f=
...
f.get(Object);//过得字段的值
f.set(Object);
//设置字段的值
Class type=f.getType();
//获得字段的类型
if(type.equals(String.
class){
}
else if(...)
-----
案例:
//public String name="aaa";
Field f=clazz.getField("name"
);
System.out.println(f.get(p));
f.set(p, "XXX"
);
System.out.println(p.name);
}
//private
@Test
public void test2()
throws Exception{
Person p=
new Person();
Class clazz=Class.forName("my.reflect.Person"
);
//获得字段
Field f=clazz.getDeclaredField("password"
);
f.setAccessible(true);
System.out.println(f.get(p));
f.set(p, "XXX"
);
System.out.println(f.get(p));
}
@Test
public void test3()
throws Exception{
Class clazz=Class.forName("my.reflect.Person"
);
Field f=clazz.getField("a"
);
System.out.println(f.get(null));
//f.set(p, "XXX");
//System.out.println(f.get(p));
}