java反射:类对象的简单使用


定义一个简单的Person类,其中含有id、name、gender等基本信息,对这些属性进行get/set/tostring方法之后,进行主要测试编写:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //先将clazzPerson类对象搞到手
        Class<?> clazzPerson = Class.forName("Person");

        //getConstructors:获得所有公共类对象方法
        Constructor<?>[] cs = clazzPerson.getConstructors();
        //然后遍历它们
        for(Constructor<?> c1:cs){
            System.out.println("Constructor:" + c1);
        }
        System.out.println("_____________");
        //获得所有包含接受了private属性的方法(也就是不管你是个什么东西都要把你给搞过来)
        Constructor<?>[] cs2 = clazzPerson.getDeclaredConstructors();
        for (Constructor<?> c2 : cs2){
            System.out.println("Constructor:" + c2);
        }
        System.out.println("__________");
        //通过  参数形式  搞到特定的某个构造方法
        Constructor<?> c3 = clazzPerson.getDeclaredConstructor(int.class,String.class,String.class);
        System.out.println(c3);

        /**
         * 使用newInstance来创建类对新实体
         * 搞到手了的具体使用
         */
        Object o = c3.newInstance(1,"yuer","???");
        Person p =(Person) o;
        System.out.println(p);
    }
}
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


public class Test2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //先将clazzPerson类对象搞到手
        Class<?> clazzPerson = Class.forName("Person");

        //getConstructors:获得所有公共类对象方法
        Constructor<?>[] cs = clazzPerson.getConstructors();
        //然后遍历它们
        for(Constructor<?> c1:cs){
            System.out.println("Constructor:" + c1);
        }
        System.out.println("_____________");
        //获得所有包含接受了private属性的方法(也就是不管你是个什么东西都要把你给搞过来)
        Constructor<?>[] cs2 = clazzPerson.getDeclaredConstructors();
        for (Constructor<?> c2 : cs2){
            System.out.println("Constructor:" + c2);
        }
        System.out.println("__________");
        //通过  参数形式  搞到特定的某个构造方法
        Constructor<?> c3 = clazzPerson.getDeclaredConstructor(int.class,String.class,String.class);
        System.out.println(c3);

        /**
         * 使用newInstance来创建类对新实体
         * 搞到手了的具体使用
         */
        Object o = c3.newInstance(1,"yuer","???");
        Person p =(Person) o;
        System.out.println(p);
    }
}

优质内容筛选与推荐>>
1、awk格式化(企业实战三)
2、工作学习周总结#8
3、微信小程序计时器清除问题setInerval
4、我的第一篇博客
5、3D渲染管线的概述


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn