反射使用(一)

反射使用(一)

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class ReflectionTest {
    @Test
    public void test1() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {

        Class<Per> perClass = Per.class;
        //通过反射调用对象的构造器
        Constructor<Per> perConstructor = perClass.getConstructor(String.class, int.class);
        //通过反射创建对象
        Per tom = perConstructor.newInstance("tom", 12);
        System.out.println(tom.toString());//Per{name='tom', age=12}

        //通过反射调用对象指定的属性和方法
        Field age = perClass.getDeclaredField("age");
        age.set(tom, 20);
        System.out.println(tom.toString());//Per{name='tom', age=20}

        Method show = perClass.getDeclaredMethod("show");
        show.invoke(tom);//我是show方法

        //通过反射调用私有的结构
        //调用私有构造器
        Constructor<Per> dConstructor = perClass.getDeclaredConstructor(String.class);
        dConstructor.setAccessible(true);
        Per jerry = dConstructor.newInstance("jerry");
        System.out.println(jerry);//Per{name='jerry', age=0}

        //调用私有属性
        Field name = perClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(jerry, "tom");
        System.out.println(jerry.toString());//Per{name='tom', age=0}

        //调用私有的方法
        Method call = perClass.getDeclaredMethod("call", String.class);
        call.setAccessible(true);
        Object invoke = call.invoke(jerry, "你好!");//你好!
        System.out.println(invoke);//你好!


    }

    //获取Class实例的四种方式
    @Test
    public void test2() throws ClassNotFoundException {
        //方式1:调用运行时的属性.class
        Class perClazz1 = Per.class;
        System.out.println(perClazz1);//class Per

        //方式二:通过运行时类的对象
        Per per = new Per();
        Class perClazz2 = per.getClass();
        System.out.println(perClazz2);//class Per

        //方式三:调用Class的静态方法:forName(String classPath)
        Class perClazz3 = Class.forName("Per");//指明具体包下的类
        System.out.println(perClazz3);//class Per

        //方法四:使用类的加载器ClassLoader
        ClassLoader classLoader = ReflectionTest.class.getClassLoader();
        Class perClazz4 = classLoader.loadClass("Per");
        System.out.println(perClazz4);//class Per


        //Class的实例对应着一个运行时类
        System.out.println(perClazz1 == perClazz2);//true
        System.out.println(perClazz1 == perClazz3);//true
        System.out.println(perClazz1 == perClazz4);//true
        System.out.println(perClazz2 == perClazz3);//true
        System.out.println(perClazz2 == perClazz4);//true
        System.out.println(perClazz3 == perClazz4);//true


    }

    /*
    哪些类型可以有Class对象?
    (1)class: 外部类,成员(成员内部类,静态内部类),局部内部类,匿名内部类
    (2)interface:接口
    (3)[]:数组
    (4)enum:枚举
    (5)annotation:注解@interface
    (6)primitive type:基本数据类型
    (7)void
    */
    @Test
    public void test3() {
        Class<Object> objectClass = Object.class;
        Class<Comparable> comparableClass = Comparable.class;
        Class<String> stringClass = String.class;
        Class<int[]> aClass = int[].class;
        Class<ElementType> elementTypeClass = ElementType.class;
        Class<Override> overrideClass = Override.class;
        Class<Integer> integerClass = int.class;
        Class<Void> voidClass = void.class;
        Class<Class> classClass = Class.class;

        int[] ints = new int[10];
        int[] ints1 = new int[100];
        Class<? extends int[]> aClass1 = ints.getClass();
        Class<? extends int[]> aClass2 = ints1.getClass();
        //只要元素类型与维度一样就是同一个Class
        System.out.println(aClass1 == aClass2);//true

    }

    //加载器
    @Test
    public void test4() {
        //对于自定义类,使用系统类加载器进行加载
        ClassLoader classLoader = ReflectionTest.class.getClassLoader();
        System.out.println(classLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2

        //调用系统类加载的getParent():获取扩展类加载器
        ClassLoader parent = classLoader.getParent();
        System.out.println(parent);//sun.misc.Launcher$ExtClassLoader@28a418fc

        //调用扩展类加载器的getParent():无法获取引导类加载器
        //引导类加载器主要负责加载java的核心类库,无法加载自定义类
        ClassLoader parent1 = parent.getParent();
        System.out.println(parent1);//null

        ClassLoader classLoader1 = String.class.getClassLoader();
        System.out.println(classLoader1);//null
    }

    //使用ClassLoader加载配置文件
    @Test
    public void test5() throws IOException {
        Properties properties = new Properties();
        //方式一
        FileInputStream fis = new FileInputStream("F:\\Lean\\Text\\src\\main\\resources\\jdbc.properties");
        properties.load(fis);

        String name1 = properties.getProperty("name");
        String password1 = properties.getProperty("password");
        System.out.println(name1 + ":" + password1);//Tom:12

        //方式二
        ClassLoader loader = ReflectionTest.class.getClassLoader();
        //配置文件默认识别为当前module的src下
        InputStream resource = loader.getResourceAsStream("jdbc.properties");
        properties.load(resource);

        String name2 = properties.getProperty("name");
        String password2 = properties.getProperty("password");
        System.out.println(name2 + ":" + password2);//Tom:12


    }

    //创建对应运行时类对象
    @Test
    public void test6() throws IllegalAccessException, InstantiationException {
        Class<Per> perClass = Per.class;
        //newInstance():创建对应运行时类的对象,内部调用空参构造器创建对象(必须提供空参public构造器)
        Per o =  perClass.newInstance();
        System.out.println(o);//Per{name='null', age=0}

    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值