java--反射

来源:互联网 发布:appstore软件更新不了 编辑:程序博客网 时间:2024/05/21 18:40

反射能创建实例对象,但是比new 更加能解决耦合性



1)反射调用构造函数

java.lang.Class<T>里:

取得全部构造:public Constructor<?>[] getConstructors() throws SecurityExceptio

取得一个指定参数顺序的构造:public Constructor<T> getConstructor(Class<?>... parameterTypes)

                                                throws NoSuchMethodException, SecurityException    


java.lang.reflect.Constructor<T>里:

实例化对象方法:public T newInstance(Object... initargs)

                     throws InstantiationException,
                     IllegalAccessException,
                     IllegalArgumentException,
                     InvocationTargetException



public class test{    public static void main(String args[]) throws Exception {        Class<?> cls = Class.forName("com.lw.Book");        Object obj = cls.newInstance(); //调用无参构造函数        Constructor<?> con = cls.getConstructor(String.class,double.class);        Object o1 = con.newInstance("java开发",34.5); //实例化对象        Constructor<?> con1 = cls.getConstructor();        Object o2 = con1.newInstance();    }}class Book {    private String title;    private double price;    public Book(String title,double price){        this.price = price;        this.title = title;    }    public Book(){        this.title = "我是默认书名称";        this.price =0.0;    }    @Override    public String toString() {        return "名称:" + this.title + ",价格:" + this.price;    }}




2)反射调用成员函数

java.lang.class<T>

取得一个类中的所有方法:public Method[] getMethods()
                                         throws SecurityException

取得指定的方法:public Method getMethod(String name,Class<?>... parameterTypes)
                          throws NoSuchMethodException,SecurityException


java.lang.reflect.Method里:

调用方法:public Object invoke(Object obj,Object... args)

              throws IllegalAccessException,IllegalArgumentException,    InvocationTargetException
                     
                    

public class test{    public static void main(String args[]) throws Exception {        Class<?> cls = Class.forName("com.lw.Book");        Object obj = cls.newInstance(); //调用无参构造函数        String fieldName = "title";//要操作的成员        Method setMet = cls.getMethod("setTitle",String.class);        Method getMet = cls.getMethod("get"+initcap(fieldName));        setMet.invoke(obj,"java开发"); //调用方法        System.out.println(getMet.invoke(obj));        System.out.println(obj);    }    public static String initcap(String str){ //首字母大写        return str.substring(0,1).toUpperCase() + str.substring(1);    }}class Book {    private String title;    private double price;    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    @Override    public String toString() {        return "名称:" + this.title + ",价格:" + this.price;    }}


3)反射调用成员

java.lang.class<T>里:

取得全部成员:public Field[] getDeclaredFields() throws SecurityException

取得指定成员:public Field getDeclaredField(String name) throws NoSuchFieldException,SecurityException


java.lang.reflect.Field里:

取得属性内容:public Object get(Object obj)
throws IllegalArgumentException,IllegalAccessException

设置属性的内容:public void set(Object obj,Object value)
                          throws IllegalArgumentException,IllegalAccessException


Field 从java.lang.rflect.AccessibleObject里继承了:

设置是否封装:public static void setAccessible(AccessibleObject[] array,boolean flag)
                       throws SecurityException

构造函数和普通成员函数也能取消封装,很少这样做


public class test{    public static void main(String args[]) throws Exception {        Class<?> cls = Class.forName("com.lw.Book");        Object obj = cls.newInstance(); //调用无参构造函数        Field titleField = cls.getDeclaredField("title");        titleField.setAccessible(true); //封装取消        titleField.set(obj,"Java开发"); //相当于Book类对象.title = "Java开发"        System.out.println(titleField.get(obj));    }}class Book {    private String title;}





fffdfs