反射

反射:

  通过.class字节码文件,可以获取类中的所有的内容(包括方法、属性、赋值等操作)。

获取反射类的方法

  * 已知类和对象的情况下

     1) 类名.class

     2) 对象.getClass()

  * 未知类和对象的情况下

     1) Class.forName("包名.类名")

package cn.itcast.reflect;

public class Person {
    
    public Person() {}
    
    public Person(String name) {
        this.name = name;
    }
    
    public Person(int id) {
        this.id = id;
    }
    
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
    
    private int id;
    private String name;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
package cn.itcast.reflect;

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

import org.junit.Test;

/**
 * 了解反射
 * @author Administrator
 *
 */
public class Demo1 {
    
    @Test
    public void run() throws Exception{
        // 三种方式获取class对象
        // 类名
        Class clazz1 = Person.class;
        // 通过实例
        Class clazz2 = new Person().getClass();
        // Class.forName
        Class clazz3 = Class.forName("cn.itcast.reflect.Person");
    }
    
    /**
     * 获取构造的对象
     * @throws Exception 
     */
    @Test
    public void run2() throws Exception{
        // 获取Person的Class对象
        Class clazz = Class.forName("cn.itcast.reflect.Person");
        // clazz.getConstructors();        // 获取所有的构造方法
        // 创建实例
        // Person p = (Person) clazz.newInstance();    // 创建实例,相对于调用无参数的构造方法
        // 获取有参数的构造器
        Constructor c = clazz.getConstructor(int.class,String.class);
        Person p2 = (Person) c.newInstance(1,"美美");
        System.out.println(p2.getName());
    }
    
    /**
     * 获取属性的对象
     * @throws Exception 
     */
    @Test
    public void run3() throws Exception{
        // 获取Person的Class对象
        Class clazz = Class.forName("cn.itcast.reflect.Person");
        // 声明实例
        Person p = (Person) clazz.newInstance();
        // 获取属性
        // clazz.getField("name");        // 获取是公有的name的属性
        
        Field name = clazz.getDeclaredField("name"); // 可以获取公有和私有都能获取
        name.setAccessible(true);        //    设置操作属性     
        name.set(p, "郭美美");            // p.name = "郭美美";
        
        System.out.println(name.get(p));
    }
    
    /**
     * 获取方法
     * @throws Exception
     */
    @Test
    public void run4() throws Exception{
        // 获取Person的Class对象
        Class clazz = Class.forName("cn.itcast.reflect.Person");
        // 声明实例
        Person p = (Person) clazz.newInstance();
        Method m = clazz.getDeclaredMethod("setName", String.class);
        m.setAccessible(true);
        m.invoke(p, "凤姐");        // p.setName = "凤姐";
        
        System.out.println(p.getName());
    }

}

 

posted on 2019-06-12 11:16  backend  阅读(129)  评论(0编辑  收藏  举报

导航