java反射学习

来源:互联网 发布:负离子吹风机 知乎 编辑:程序博客网 时间:2024/05/16 17:01

java反射很有意思,在知乎上找到一个讲解的不错的教程
https://www.zhihu.com/question/24304289

一,取得Class类对象的方法

public static Class<?> forName(String className) throws ClassNotFoundException;

二,通过反射实例化对象
public T newInstance() throws InstantiationException, IllegalAccessException;

三,反射的应用(获取类的构造方法、普通方法、属性)
3.1 获取构造函数
取得一个类的全部构造:

public Constructor<?>[] getConstructors() throws SecurityException

取得一个类的指定参数构造:

public Constructor<T> getConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

3.2 获取普通方法
取得全部方法:public Method[] getMethods() throws SecurityException;

·取得指定方法:

public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

发现以上的方法返回的都是java.lang.reflect.Method类的对象。

3.3 获取成员
取得本类的全部成员:

public Field[] getDeclaredFields() throws SecurityException;

·取得指定的成员:

public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException;

这两个方法的返回值类型是java.lang.reflect.Field类的对象,下面首先观察如何取得一个类之中的全部属性。

感谢原创作者,等回到自己电脑上时,必须亲自敲代码验证下正确性。

package subClass;import java.awt.Window;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;class Person{    private String name;    private int age;    public Person()    {    }    public Person(String name,int age)    {        this.name = name;        this.age = age;    }    public void setName(String name){        this.name = name;    }    public String getName() {        return this.name;    }    public void setAge(int age)    {        this.age = age;    }    public int getAge()    {        return this.age;    }    @Override    public String toString(){        return"Person [name="+ name+ ", age="+ age+ "]";    }}public class SubClass  {    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException{        Class<?> cls = Class.forName("subClass.Person") ;        //获取全部属性        Field[] field = cls.getDeclaredFields();        for (int i = 0; i < field.length; i++) {            System.out.println(field[i]);        }        //实例化对象        Object obj = cls.newInstance();        String attribute = "name";        Method setMet = cls.getMethod("setName",String.class);        setMet.invoke(obj, "张三");        Method getMet = cls.getMethod("getName");        String strName = (String) getMet.invoke(obj);        Method setAgeMet = cls.getMethod("setAge", int.class);        setAgeMet.invoke(obj, 100);        Method getAgeMet = cls.getMethod("getAge");        int age = (int) getAgeMet.invoke(obj);        System.out.println("设置后的年龄是:"+age);        System.out.println(strName);        Field nameFiled = cls.getDeclaredField("name");        //由于需要破坏类的封装性,所以这里解除封装        nameFiled.setAccessible(true);        nameFiled.set(obj, "李四");        System.out.println("name after set function is "+nameFiled.get(obj));    }}class Employee{    private String id;    private String name;    private int age;    private Employee() {    }    public  Employee(String name,int age) {        this.id = "1001";        this.name = name;        this.age = age;    }    private String getID(){        return this.id;    }    //method    public void sayHello(){        System.out.println("Hello,name="+name+" age = "+age);    }}
0 0
原创粉丝点击