反射、基础

一、获取字节码的四种方式

 1)通过对象的getClass 方法

        // 1. 通过getClass 方法获取
        AliPay aliPay = new AliPay();
        Class<? extends AliPay> c1 = aliPay.getClass();

2)通过对象内置的Class方法

        // 2. 通过内置的class方法
        Class<AliPay> c2 = AliPay.class;

3)通过Class类静态的forName方法

        // 方式一 与方式二 不常用
        // 3. Class类提供的静态方法 forName
        // 一般使用这个方式
        Class<?> c3 = Class.forName("com.lmh.weChat");

4)通过类的加载器 classLoader

        // 4. 利用类的加载器 classLoader
        ClassLoader classLoader = text01.class.getClassLoader();
        classLoader.loadClass("com.lmh.weChat");

二、获取构造器

1)  getConstructor(Class parameter) 获取修饰符为public的某一个构造器

2) getConstructor() 获取修饰符为public的 所有构造器

3) getDeclaredConstructor(Class parameter)  获取任意一个构造器

4)getDeclaredAnnotations() 获取所有的构造器

        // 1. 先获取字节码信息
        Class<?> aClass = Class.forName("com.lmh.weChat");
        // 2. getConstructor(Class parameter) 获取修饰符为public的某一个构造器
        Constructor<?> c = aClass.getConstructor();
        // 3. getConstructors() 获取修饰符为public的全部构造器
        Constructor<?>[] c1 = aClass.getConstructors();
        for (Constructor co : c1){
            System.out.println(co);
        }
        // 4. getDeclaredConstructor(Class parameter) 获取任何一个 构造器
        Constructor<?> dc = aClass.getDeclaredConstructor(int.class);
        // 5. getDeclaredAnnotations() 获取全部的构造器
        Annotation[] dc1 = aClass.getDeclaredAnnotations();

三、创建实例

  newInstance() 创建对应的构造器

        // 创建实例newInstance() 创建对应的构造器
        Object o = c.newInstance();
        // 创建 有参的实例
        dc.newInstance(1);

四、获取属性、类型、修饰符和对属性进行赋值

package com.lmh;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * @author codelmh
 * @data 2021/11/28
 */
public class text03 {
    public static void main(String[] args) throws Exception {
        // 1. 获取字节码信息
        Class<?> clazz = Class.forName("com.lmh.weChat");
        // 2. getFields() 获取所有public修饰的属性(包含父类)
        Field[] fields = clazz.getFields();
        // 3. getField(String name) 获取修饰符为public 名字为name的属性
        Field id1 = clazz.getField("iid");
        // 3. getDeclaredFields() 获取所有属性(包含父类)
        Field[] declaredFields = clazz.getDeclaredFields();
        // 4. getDeclaredField(String name) 获取修饰符为public 名字为name的属性
        Field id = clazz.getDeclaredField("id");
        System.out.println("----------------------------------------");
        //获取属性的名字
        String name = id.getName();
        // 获取属性的类型
        Class<?> type = id.getType();
        // 获取属性的修饰符
        int modifiers = id.getModifiers();
        System.out.println(Modifier.toString(modifiers));
        System.out.println("----------------------------------------");
        // 给属性赋值
        Constructor<?> con = clazz.getConstructor();
        Object o = con.newInstance();
        // 给对象o 的 id1 属性 赋值 99
        id1.set(o , 99);
    }
}

五、获取方法和调用方法

package com.lmh;

import java.lang.reflect.Method;

/**
 * @author codelmh
 * @data 2021/11/28
 */
public class text04 {
    public static void main(String[] args) throws Exception {
        // 1. 获取字节码
        Class<?> clazz = Class.forName("com.lmh.weChat");
        System.out.println("---------获取修饰符为 public的方法-------------");
        // 获取被(public)修饰的所有方法
        Method[] methods = clazz.getMethods();
        // 被(public)修饰的 getMethod(String name, Class<?>... parameterTypes)
        // name --> 方法名称  parameterTypes --> 方法的参数类型 可以变长
        Method payOnline = clazz.getMethod("payOnline");

        System.out.println("---------获取任意方法-------------");
        // 任意 获取方法名为payOnline的方法
        Method payOnline1 = clazz.getDeclaredMethod("payOnline");
        // 获取任意方法
        Method[] declaredMethods = clazz.getDeclaredMethods();

        System.out.println("---------调用方法-------------");
        Object o = clazz.newInstance();
        // invoke(Object obj, Object... args)
        // 调用o对象的 payOnline方法,并且传参 args
        payOnline.invoke(o);
    }
}

六、获取接口、所在包、注解

package com.lmh;

import java.lang.annotation.Annotation;

/**
 * @author codelmh
 * @data 2021/11/28
 */
public class text06 {
    public static void main(String[] args) throws Exception {
        //获取字节码
        Class<?> clazz = Class.forName("com.lmh.weChat");
        // 获取接口
        System.out.println("---------------获取接口-------------------");
        Class<?>[] interfaces = clazz.getInterfaces();


        // 获取运行时所在包
        System.out.println("---------------获取运行时所在包-------------------");
        Package aPackage = clazz.getPackage();

        // 获取运行时注解
        System.out.println("---------------获取运行时注解-------------------");
        // 获取所有注解
        Annotation[] annotations = clazz.getAnnotations();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值