反射的API
- Class类
- Constructor类
- Field类
- Method类
java反射机制运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能成为java语言的反射机制。
Person类作为演示,其他类作为测试
package com.imooc.reflect.test;
public class Person {
public Person() {
super();
}
private String name;
private String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public void eat(){
System.out.println("吃");
}
private void run(){
System.out.println("跑");
}
public String sayHello(String name){
return "Hello"+name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
- Class类
获取到Class文件对于的class对象
package com.imooc.reflect.test;
import org.junit.Test;
public class ClassTest {
@Test
public void demo1() throws ClassNotFoundException{
//类名.class
Class clazz1=Person.class;
//对象.getClass的方法
Person person = new Person();
Class clazz2=person.getClass();
//class类forName获取
Class.forName("com.imooc.reflect.test.Person");
}
}
- Constructor类
构造方法的获取,分是否有参数
package com.imooc.reflect.test;
import org.junit.Test;
import java.lang.reflect.Constructor;
public class ConstructorTest {
@Test
public void demo1() throws Exception{
//获取类字接码文件对应的对象
Class class1= Class.forName("com.imooc.reflect.test.Person");
Constructor c = class1.getConstructor();
Person person = (Person) c.newInstance();//相当于 Person person=new Person()
}
@Test
/**
* 有参数构造方法
*/
public void demo2() throws Exception{
//获取类字接码文件对应的对象
Class class1= Class.forName("com.imooc.reflect.test.Person");
Constructor c = class1.getConstructor(String.class,String.class);
Person person = (Person) c.newInstance("张三","男");
System.out.println(person);
}
}
- Field类
Field类代表某个类中的一个成员变量,并提供动态访问权限。权限对于非public属性,需要设置可以取得所有声明的属性。
package com.imooc.reflect.test;
import org.junit.Test;
import java.lang.reflect.Field;
public class FieldTest {
@Test
public void demo1() throws Exception{
Class class1=Class.forName("com.imooc.reflect.test.Person");
//name是private,需要设置获取权限
Field field = class1.getDeclaredField("name");
Person p = (Person) class1.newInstance();
field.setAccessible(true);
field.set(p,"李四");
Object obj = field.get(p);
System.out.println(obj);
}
}
- Method类
Method类代表某个类中的一个成员方法;如果非public,也需要进行权限设置。
package com.imooc.reflect.test;
import org.junit.Test;
import java.lang.reflect.Method;
public class MethodTest {
@Test
public void demo1() throws Exception{
Class class1=Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class1.newInstance();
Method method = class1.getMethod("eat");
method.invoke(person);
}
@Test
public void demo2() throws Exception{
Class class1=Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class1.newInstance();
Method method = class1.getDeclaredMethod("run");
method.setAccessible(true);
method.invoke(person,null);
}
@Test
public void demo3() throws Exception{
Class class1=Class.forName("com.imooc.reflect.test.Person");
Person person = (Person) class1.newInstance();
// public可以设置访问权限,也可以不设置
// Method method = class1.getDeclaredMethod("sayHello", String.class);
// method.setAccessible(true);
Method method = class1.getMethod("sayHello", String.class);
Object obj = method.invoke(person, "Tom");
System.out.println(obj);
}
}
测试类使用的过程中
pom.xml加入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>