什么是java的反射机制:
Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
Java反射常用对象
Class: Class类的实例表示正在运行的java应用程序中的类和接口
Constructor: 关于类的单个构造方法的信息以及对它的访问权限
Field: 提供有关类或接口的单个字段的信息,以及对它的动态访问权限
Method: 提供关于类或接口上单独某个方法的信息
Class类:
public class Person {
public Person() {
super();
}
public Person(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
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 + "]";
}
}
public class ClassTest {
@Test
/**
* 获得Class对象
* 1.通过类名.class
* 2.对象.getClass()
* 3.Class.forName();
*/
public void demo1() throws ClassNotFoundException{
// 1.通过类名.class的方式
Class clazz1 = Person.class;
// 2.通过对象.getClass()的方式
Person person = new Person();
Class clazz2 = person.getClass();
// 3.Class类forName();获得(推荐)
Class clazz3 = Class.forName("com.dodoke.reflect.test.Person");
}
}
Constructor类
public class ConstructorTest {
/**
* 获得无参数的构造方法
*/
public void demo1() throws Exception{
// 获得类的字节码文件对应的对象:
Class class1 = Class.forName("com.dodoke.reflect.test.Person");
Constructor c = class1.getConstructor();
Person person = (Person) c.newInstance();// 相当于Person person = new Person();
// person.eat();
}
/**
* 获得有参数的构造方法
*/
public void demo2() throws Exception{
Class class1 = Class.forName("com.dodoke.reflect.test.Person");
Constructor c = class1.getConstructor(String.class,String.class);
Person person = (Person) c.newInstance("张三","男");// Person person = new Person("张三","男");
System.out.println(person);
}
}
Field类
public class FieldTest {
// 测试公有的属性
public void demo1() throws Exception{
// 获得Class
Class class1 = Class.forName("com.dodoke.reflect.test.Person");
// 获得属性:
Field field = class1.getField("name");
// 操作属性: p.name = "";
Person p = (Person) class1.newInstance();
field.set(p, "李四");// p.name = "李四";
Object obj = field.get(p);
System.out.println(obj);
}
// 测试私有的属性
public void demo2() throws Exception{
// 获得Class
Class class1 = Class.forName("com.dodoke.reflect.test.Person");
// 获得私有的属性
Field field = class1.getDeclaredField("sex");
// 操作属性:
Person p = (Person) class1.newInstance();
// 私有属性,需要设置一个可访问的权限:
field.setAccessible(true);
field.set(p, "男");
// 获取值:
Object obj = field.get(p);
System.out.println(obj);
System.out.println(p);
}
}
Method类
public class MethodTest {
// 测试公有的方法
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); // person.eat();
}
// 测试私有的方法
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);
}
// 测试私有的方法带参数
public void demo3() throws Exception{
Class class1 = Class.forName("com.imooc.reflect.test.Person");
// 实例化:
Person person = (Person) class1.newInstance();
// 获得该方法:
Method method = class1.getDeclaredMethod("sayHello", String.class);
// 设置访问权限:
method.setAccessible(true);
// 执行:
Object obj = method.invoke(person, "Tom");
System.out.println(obj);
}
}