黑马程序员-反射

2015-06-27  本文已影响169人  狼孩
反射

1.Class类

        String str = "abc";
        Class cls1 = str.getClass();
        Class cls2 = String.class;
        Class cls3 = Class.forName("java.lang.String");
        System.out.println(cls1.isPrimitive()); //是否是基本数据类型
        System.out.println(int.class == Integer.TYPE); //int包装数据类型类的类型和int对应的字节码相同
        System.out.println(int.class == Integer.class); //int和Integer的字节码不相同
        System.out.println(int[].class.isArray()); //数组类型的Class实例对象

2.反射介绍

构造方法的反射:

        Constructor[] con = Class.forName("java.lang.String").getConstructors();//获取此类的所有构造方法
        Constructor con1 = String.class.getConstructor(StringBuffer.class);//获取单个指定构造方法

成员变量的反射:

package com.sergio.NewTecl;

import java.io.File;
import java.lang.reflect.Field;

/**
 * 成员反射
 * Created by Sergio on 2015-06-05.
 */
public class ReflectField {
    public static void main(String[] args) throws Exception {
        ReflectTest rt = new ReflectTest(3, 5);
        //获取rt字节码对象上的x变量值.获取私有变量的值
        Field fieldX = rt.getClass().getDeclaredField("x");
        //设置私有变量获取后可以使用了
        fieldX.setAccessible(true);
        System.out.println(fieldX.get(rt));
        //获取的是共有变量
        Field fieldY = rt.getClass().getField("y");
        System.out.println(fieldY.get(rt));

        changeStringValue(rt);
        System.out.println(rt);
    }

    //更改rt对象中变量的某些值
    private static void changeStringValue(Object object) throws IllegalAccessException {
        Field[] fields = object.getClass().getFields();
        for (Field field : fields) {
            if (field.getType() == String.class) {
                String oldValue = (String) field.get(object);
                String newValue = oldValue.replace('b', 'a');
                field.set(object, newValue);
            }
        }
    }
}


class ReflectTest {
    private int x;
    public int y;
    //更改b为a
    public String str1 = "ball";
    public String str2 = "basketball";
    public String str3 = "itcast";


    public ReflectTest(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "ReflectTest{ + str1='" + str1 + '\'' + ", str2='" + str2 + '\'' + ", str3='" + str3
            + '\'' +
            '}';
    }
}

成员方法反射

        String str1 = "abc";
        //str1.charAt(1)调用下面方法方式
        Method method = String.class.getMethod("charAt", int.class);
        //打印1位置的字符,method.invoke(null, 1)静态方法调用
        System.out.println(method.invoke(str1, 1));

数组的反射

package com.sergio.NewTecl;

import java.lang.reflect.Array;

/**
 * 数组反射实例
 * Created by Sergio on 2015-06-06.
 */
public class ReflectArrayTest {
    public static void main(String[] args) {
        int[] a1 = new int[] {1, 2, 3};
        String[] a4 = new String[] {"xya"};

        printObject(a1);
        printObject(a4);

    }
    //打印数组中的元素
    private static void printObject(Object obj) {
        Class clazz = obj.getClass();
        if (clazz.isArray()) {
            int length = Array.getLength(obj);
            for (int i = 0; i < length; i++) {
                System.out.println(Array.get(obj, i));
            }
        } else {
            System.out.println(obj);
        }
    }
}
类加载器与反射
package com.sergio.NewTecl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;

/**
 * 反射加载外部文件操作
 * Created by Sergio on 2015-06-06.
 */
public class ReflectCollection {
    public static void main(String[] args) throws Exception {
        //        Collection collections = new ArrayList<>();
        InputStream ips = new FileInputStream("config.properties");
        //类加载方法,加载外部文件配置的主要方式
        //InputStream ips2 = ReflectCollection.class.getClassLoader().getResourceAsStream("config.properties");

        Properties props = new Properties();
        props.load(ips);
        ips.close();

        String className = props.getProperty("className");
        //创建ArrayList构造方法实例,加载的外部文件为className=java.util.ArrayList
        Collection collections = (Collection) Class.forName(className).newInstance();
        ReflectTest rt = new ReflectTest(3, 5);
        ReflectTest rt1 = new ReflectTest(3, 5);
        ReflectTest rt2 = new ReflectTest(3, 5);
        collections.add(rt);
        collections.add(rt1);
        collections.add(rt2);

        System.out.println(collections.size());
    }
}


class ReflectTest1 {
    private int x;
    public int y;

    public ReflectTest1(int x, int y) {
        this.x = x;
        this.y = y;
    }
}


配置文件信息:className=java.util.ArrayList

反射与泛型

package com.sergio.NewTecl;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.Vector;

/**
 * 通过反射获取泛型方法的参数类型
 * Created by Sergio on 2015-06-12.
 */
public class GenericDao {
    public static void applyVector(Vector<Date> v1) {

    }

    public static void main(String[] args) throws Exception {
        //要调用哪个泛型方法
        Method method = GenericDao.class.getMethod("applyVector()", Vector.class);
        Type[] types = method.getGenericParameterTypes();//获取泛型方法
        ParameterizedType pType = (ParameterizedType) types[0];//转化为参数类型
        System.out.println(pType.getRawType());//返回被限制的泛型类型
        System.out.println(pType.getActualTypeArguments());////返回泛型实际参数类型
    }
}
上一篇 下一篇

猜你喜欢

热点阅读