类似PHP中的var_dump,Java中的输出调试函数

public void helpFunc(Object o) {
        String className = o.getClass().getName();
        Field[] fields = o.getClass().getDeclaredFields();
        System.out.println("类" + className + "中声明的field属性");
        for (int i = 0; i < fields.length; ++i) {
            fields[i].setAccessible(true);
            try {
                System.out.println(fields[i].getName() + "-" + fields[i].toString() + "-" + fields[i].get(o));
            } catch (Exception e) {
                System.out.println(e);
            }
        }

        Class[] classes = o.getClass().getDeclaredClasses();
        System.out.println("类" + className + "中声明的类");
        for (int i = 0; i < classes.length; ++i) {
            try {
                System.out.println(classes[i].getName() + "-" + classes[i].toString());
            } catch (Exception e) {
                System.out.println(e);
            }
        }

        Method[] methods = o.getClass().getDeclaredMethods();
        System.out.println("类" + className + "中声明的方法");
        for (int i=0; i < methods.length; ++i) {
            try {
                System.out.println(methods[i].getName() + "-" + methods[i].toString());
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }

2017年3月30日17:22:54更新

/**
     * 通过按位与运算,计算需要打印的选项
     * 0或1 打印类的属性
     * 0或2 打印类中声明的类
     * 0或4 打印类的方法
     * 例如3表示 打印类的属性和类中声明的类
     * @param o    需要打印的对象
     * @param sum  需要打印的和值
     */
    public static void helpFunc(Object o, int sum) {
        String className = o.getClass().getName();
        if ((sum & 1) == 1) {
            Field[] fields = o.getClass().getDeclaredFields();
            System.out.println("类" + className + "中声明的field属性");
            for (int i = 0; i < fields.length; ++i) {
                fields[i].setAccessible(true);
                try {
                    System.out.println(fields[i].getName() + "-" + fields[i].toString() + "-" + fields[i].get(o));
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

        if ((sum & 2) == 2) {
            Class[] classes = o.getClass().getDeclaredClasses();
            System.out.println("类" + className + "中声明的类");
            for (int i = 0; i < classes.length; ++i) {
                try {
                    System.out.println(classes[i].getName() + "-" + classes[i].toString());
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

        if ((sum & 4) == 4) {
            Method[] methods = o.getClass().getDeclaredMethods();
            System.out.println("类" + className + "中声明的方法");
            for (int i = 0; i < methods.length; ++i) {
                try {
                    System.out.println(methods[i].getName() + "-" + methods[i].toString());
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值