Java的反射机制的详细应用

package com.at221;

import java.io.Serializable;
import java.lang.reflect.*;

import org.junit.Test;

public class TestReflection {
    @Test
    public void test1() throws Exception{
        Class<?> class1 = null;
        Class<?> class2 = null;
        Class<?> class3 = null;
        //对象实例化的第一种方式:
        class1 = Class.forName("com.at221.Person");
//        Person person = (Person)class1.newInstance();
        
        //对象实例化的第二种方式:
        
        class2 = new Person().getClass();
        
        //第三种对象实例化的方式:
        
        class3 = Person.class;
        
        System.out.println(class1);
        System.out.println(class2);
        System.out.println(class3);
        
    }
    
    /*运行结果:
     *     class com.at221.Person
        class com.at221.Person
        class com.at221.Person

     * 
     */
    
    //获取类的父类和接口:
    @Test
    public void test2() throws Exception{
        Class<?> class1 = Class.forName("com.at221.Person");
        
        Class<?> parentclass = class1.getSuperclass();
        System.out.println(parentclass.getName());
        
        System.out.println("----------------");
        
        Class<?> inte[] = class1.getInterfaces();
        for(int i = 0; i < inte.length; i++){
            System.out.println(inte[i].getName());
        }
    }
    /*
     * 运行结果:
     *     com.at221.Creture
        ----------------
        java.io.Serializable

     */
    
    
    //获取类的属性和属性的作用域和类型:
    @Test
    public void test3(){
        Class<?> class2 = new Person().getClass();
        Field[] fields = class2.getDeclaredFields();
        
        for(int i = 0; i < fields.length; i++){
            Class<?> types = fields[i].getType();
            int mo = fields[i].getModifiers();
            System.out.println(Modifier.toString(mo) + " " + 
                                types.getName() + " " + fields[i].getName() + "
");
        }
        /*运行结果:
         *     private static final long serialVersionUID

            private java.lang.String name
            
            private int age
            
            private java.lang.String nation
         * 
         */
        
        //获取构造器的各种信息:
        
        Class<?> class3 = new Person().getClass();
        Constructor<?> construtor[] = class3.getDeclaredConstructors();
        
        for(int i = 0; i < construtor.length; i++){
            int mo = construtor[i].getModifiers();
            Class<?> types[] = construtor[i].getParameterTypes();
            System.out.println(Modifier.toString(mo) + " " + construtor[i].getName());
            System.out.println(i + ": ");
            for(int j = 0; j < types.length; j++){
                System.out.print(types[j].getName() + " ");    
            }
            System.out.println("--------------");
        }
        /*运行结果:
         *     public com.at221.Person
            0: 
            --------------
            public com.at221.Person
            1: 
            java.lang.String int java.lang.String --------------
         * 
         */
     }
    
    @Test
    public void test4(){
        //获取类中的方法:
        Class<?> class4 = Person.class;
        Method methods[] = class4.getDeclaredMethods();
        for(int i = 0; i < methods.length; i++){
            
            int mod = methods[i].getModifiers();//获取函数的作用域;
            Class<?> returnTypes = methods[i].getReturnType();//q就函数的返回值;
            
            System.out.print("method[" + i + "] :" + Modifier.toString(mod) + " " + 
                                " " + returnTypes.getName() + " " + methods[i].getName() + " ( ");
            Class<?> parameterType[] = methods[i].getParameterTypes();//获取函数的形参类型;
            for(int j = 0; j < parameterType.length; j++){
                System.out.print(parameterType[j].getName() + " ");
            }
            System.out.println(")");
            
        }
    }
    /*运行结果:
     *     method[0] :public  java.lang.String toString ( )
        method[1] :public  java.lang.String getName ( )
        method[2] :public  void setName ( java.lang.String )
        method[3] :public  int getAge ( )
        method[4] :public  void setAge ( int )
        method[5] :public  java.lang.String getNation ( )
        method[6] :public  void setNation ( java.lang.String )

     * 
     */
}


class Person extends Creture implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    private String nation;
    
    public Person() {
        super();
    }

    public Person(String name, int age, String nation) {
        super();
        this.name = name;
        this.age = age;
        this.nation = nation;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", nation=" + nation + "]";
    }
}

class Creture{
    public Creture(){
        super();
    }
    
    public void show(){
        System.out.println("!!!i am a creture!!!");
    }
}
原文地址:https://www.cnblogs.com/zhaoningzyn/p/6828851.html