java反射回顾

1.反射获取方法返回类型,参数,异常

public static void main(String[] args) {
        Class demo = null;
        try {
            demo = Class.forName("com.java.reflect.People");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method[] methods = demo.getMethods();
        for (int i = 0; i < methods.length; i++) {
            //获取方法返回类型
            Class returnType = methods[i].getReturnType();
            //获取方法参数
            Class para[] = methods[i].getParameterTypes();
            int temp = methods[i].getModifiers();
            System.out.print(Modifier.toString(temp));
            System.out.print(returnType.getName() + " ");
            System.out.print(methods[i].getName() + " ");
            System.out.print("(");
            for (int j = 0; j < para.length; j++) {
                System.out.print(para[j].getName() + " " + "arg" + j);
                if (j > args.length - 1) {
                    System.out.print(',');
                }
            }
            //获取方法抛出的异常
            Class[] exce = methods[i].getExceptionTypes();
            if (exce.length > 0) {
                System.out.print(") throws ");
                for (int k = 0; k < exce.length; ++k) {
                    System.out.print(exce[k].getName() + " ");
                    if (k < exce.length - 1) {
                        System.out.print(",");
                    }
                }
            } else {
                System.out.print(")");
            }
            System.out.println();
        }
    }

运行结果

publicjava.lang.String toString ()
publicvoid setAge (int arg0,)
publicjava.lang.String getFirstName ()
publicvoid setFirstName (java.lang.String arg0,)
publicjava.lang.String getLastName ()
publicvoid setLastName (java.lang.String arg0,)
publicjava.lang.String say (java.lang.String arg0,)
publicint getAge ()
publicvoid sayYourCountry ()
public finalvoid wait () throws java.lang.InterruptedException 
public finalvoid wait (long arg0,int arg1,) throws java.lang.InterruptedException 
public final nativevoid wait (long arg0,) throws java.lang.InterruptedException 
publicboolean equals (java.lang.Object arg0,)
public nativeint hashCode ()
public final nativejava.lang.Class getClass ()
public final nativevoid notify ()
public final nativevoid notifyAll ()

2.获取类的属性

2.1获取本类的属性

Field[] field = demo.getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
            int m = field[i].getModifiers();
            System.out.print("属性修饰符->" + Modifier.toString(m));
            Class type = field[i].getType();
            System.out.print(",返回类型->"+type.getName());
            System.out.println(",属性名->"+field[i].getName());
            System.out.println();
        }

运行结果

属性修饰符->private,返回类型->java.lang.String,属性名->major

2.2获取父类或者接口的属性

Field[]field2=demo.getFields();
        for (int i = 0; i < field2.length; i++) {
            int m = field2[i].getModifiers();
            System.out.print("属性修饰符->" + Modifier.toString(m));
            Class type = field2[i].getType();
            int j=1+2;
            System.out.print(",返回类型->"+type.getName());
            System.out.println(",属性名->"+field2[i].getName());
            System.out.println();
        }

运行结果

属性修饰符->public,返回类型->java.lang.String,属性名->firstName

属性修饰符->public,返回类型->java.lang.String,属性名->lastName

属性修饰符->public,返回类型->int,属性名->age

3.运用反射修改属性的方法

try {
            Object obj = demo.newInstance();
            Field field = demo.getDeclaredField("major");
            field.setAccessible(true);
            field.set(obj, "计算机");
            System.out.println(field.get(obj));
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

运行结果

计算机

你可能感兴趣的:(java)