Java反射

class类的使用

/**
 * class类的使用
 */
public class demo1 {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        //ProductService的实例对象  (ProductService是一个类)
        ProductService service = new ProductService();

        //任何一个类都是class类的实例对象,这个实例对象有三种表示方式
        //第一种方式--->实际告诉我们任何一个类都有一个隐含的静态成员变量class
        Class<ProductService> class1 = ProductService.class;

        //第二种方式
        Class<? extends ProductService> class2 = service.getClass();

        //第三种方式
        Class<?> class3 = null;
        try {
            class3 = Class.forName("com.example.demo.MyAOp.service.ProductService");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        /**
         * class1,class2,class3表示ProductService类的类类型
         * 万事万物皆对象
         * 类也是对象,是class类的实例对象
         * 这种对象我们称为该类的类类型
         * 一个类只可能是Class类的一个实例对象,class1=class2=class3
         */
        System.out.println(class1==class2);
        System.out.println(class1==class3);
        System.out.println(class2==class3);


        //完全可以通过类的类类型取创建该类的对象实例(前提:需要有无参数的构造方法)
        try {
            ProductService productService1 = class1.newInstance();
            ProductService productService2 = class2.newInstance();
            ProductService productService3 = (ProductService)class3.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

获取方法信息

/**
 * 获取方法信息
 */
public class MehtedMseeage {
    public static void main(String[] args) {
        getClassMessage(new ProductService());
    }

    /**
     * 获取方法返回类型,方法名,参数类型
     * @param obj
     */
    public static void getClassMessage(Object obj) {
        //获取类的类类型
        Class<?> c = obj.getClass();
        //获取类名
        String name = c.getName();
        //获取类名,不包括包路径
        String simpleName = c.getSimpleName();
        //获取所有public方法,包括父类的
        Method[] methods = c.getMethods();
        //获取该类自己声明的所有方法,不问访问权限
        Method[] declaredMethods = c.getDeclaredMethods();
        for (Method declaredMethod:declaredMethods){
            //获取方法返回值类型,得到是类类型
            Class<?> returnType = declaredMethod.getReturnType();
            System.out.print(returnType.getSimpleName()+ "  ");
            //获取方法名
            String name1 = declaredMethod.getName();
            System.out.print(name1 + "(");
            //获取参数类型,得到类类型
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            for (Class parameterType:parameterTypes) {
                System.out.print(parameterType.getSimpleName() + ",");
            }
            System.out.println(")");
        }
    }
}

成员变量信息

/**
 * 获取成员变量
 * 成员变量也是对象
 * 是java.lang.reflect.Filed对象
 * Filed封装了关于成员变量的操作
 */
public class MemberVariables {
    public static void main(String[] args) {
        getMethodFiledMessage(new ProductService());
    }

    /**
     * 成员变量信息
     * @param obj
     */
    public static void getMethodFiledMessage(Object obj) {
        //获取类的类类型
        Class<?> c = obj.getClass();
        //获取所有public成员变量
        Field[] fields = c.getFields();
        //获取所有成员变量
        Field[] declaredFields = c.getDeclaredFields();
        for (Field field : declaredFields) {

            //获取成员变量的类型的类类型
            Class<?> type = field.getType();
            System.out.print(type.getSimpleName() + "  ");
            //成员变量名
            String name = field.getName();
            System.out.println(name);
        }
    }
}

构造函数

 /**
     * 构造函数信息
     * java.lang.Constructors种封装了构造函数信息
     * @param obj
     */
    public static void getMethodConMessage(Object obj) {
        //获取类的类类型
        Class<?> c = obj.getClass();
        //获取所有public构造函数
        Constructor<?>[] constructors = c.getConstructors();
        //获取所有构造函数
        Constructor<?>[] declaredConstructors = c.getDeclaredConstructors();
        for (Constructor constructor:declaredConstructors){
            String name = constructor.getName();
            System.out.print(name + "(");
            Class[] parameterTypes = constructor.getParameterTypes();
            for (Class parameterType:parameterTypes) {
                System.out.print(parameterType.getSimpleName() + ",");
            }
            System.out.println(")");
        }
    }

方法的反射

这里写图片描述

public class FanShe {
    public static void main(String[] args) {
        try {
            A a = new A();
            Class<? extends A> c = a.getClass();
            //获取方法,由参数名和参数列表确定
            //getDeclaredMethod获取自己声明的方法
            Method method = c.getDeclaredMethod("print",new Class[]{int.class,int.class});
            //方法的反射操作,相当a.print(1,2);
            Object invoke = method.invoke(a, 1, 2); //Object invoke = method.invoke(a,new Object[]{1,2});

            System.out.println("======------------------------===============");
            Method method2 = c.getDeclaredMethod("print",String.class,String.class);
            Object invoke2 = method2.invoke(a, new Object[]{"av","aa"});
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            System.out.println("======");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("======");
        }
    }
}


class A {
    public void print(int a,int b) {
        System.out.println(a+b);
    }

    public void print(String a,String b) {
        System.out.println(a.toUpperCase()+b.toLowerCase());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值