发表于: 2017-07-29 23:01:57

1 853


今天完成的事:

1:学习Java反射相关内容

通过一个对象获得完整的包名和类名

package net.xsoftlab.baike;
public class TestReflect {
    public static void main(String[] args) throws Exception {
        TestReflect testReflect = new TestReflect();
        System.out.println(testReflect.getClass().getName());
        // 结果 net.xsoftlab.baike.TestReflect
    }

}

public class TestReflect {
    public static void main(String[] args) throws Exception {
        Class<?> class1 = null;
        Class<?> class2 = null;
        Class<?> class3 = null;
        // 一般采用这种形式
        class1 = Class.forName("net.xsoftlab.baike.TestReflect");
        class2 = new TestReflect().getClass();
        class3 = TestReflect.class;
        System.out.println("类名称   " + class1.getName());
        System.out.println("类名称   " + class2.getName());
        System.out.println("类名称   " + class3.getName());
    }
}

通过反射机制操作某个类的属性

public class TestReflect {
    private String proprety = null;
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("net.xsoftlab.baike.TestReflect");
        Object obj = clazz.newInstance();
        // 可以直接对 private 的属性赋值
        Field field = clazz.getDeclaredField("proprety");
        field.setAccessible(true);
        field.set(obj, "Java反射机制");
        System.out.println(field.get(obj));
    }
}

获取一个对象的父类与实现的接口

public class TestReflect implements Serializable {
    private static final long serialVersionUID = -2862585049955236662L;
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("net.xsoftlab.baike.TestReflect");
        // 取得父类
        Class<?> parentClass = clazz.getSuperclass();
        System.out.println("clazz的父类为:" + parentClass.getName());
        // clazz的父类为: java.lang.Object
        // 获取所有的接口
        Class<?> intes[] = clazz.getInterfaces();
        System.out.println("clazz实现的接口有:");
        for (int i = 0; i < intes.length; i++) {
            System.out.println((i + 1) + ":" + intes[i].getName());
        }
        // clazz实现的接口有:
        // 1:java.io.Serializable
    }
}

获取某个类的全部方法

import java.io.Serializable;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

public class TestReflect implements Serializable {

    private static final long serialVersionUID = -2862585049955236662L;

    public static void main(String[] args) throws Exception {

        Class<?> clazz = Class.forName("net.xsoftlab.baike.TestReflect");

        Method method[] = clazz.getMethods();

        for (int i = 0; i < method.length; ++i) {

            Class<?> returnType = method[i].getReturnType();

            Class<?> para[] = method[i].getParameterTypes();

            int temp = method[i].getModifiers();

            System.out.print(Modifier.toString(temp) + " ");

            System.out.print(returnType.getName() + "  ");

            System.out.print(method[i].getName() + " ");

            System.out.print("(");

            for (int j = 0; j < para.length; ++j) {

                System.out.print(para[j].getName() + " " + "arg" + j);

                if (j < para.length - 1) {

                    System.out.print(",");

                }

            }

            Class<?> exce[] = method[i].getExceptionTypes();

            if (exce.length > 0) {

                System.out.print(") throws ");

                for (int k = 0; k < exce.length; ++k) {

                    System.out.print(exce[k].getName() + " ");

                    if (k < exce.length - 1) {

                        System.out.print(",");

                    }

                }

            } else {

                System.out.print(")");

            }

            System.out.println();

        }

    }

}

明天计划的事:

继续学习,基础

遇到的问题:无



返回列表 返回列表
评论

    分享到