首页 > 其他 > 详细

通过反射来输出一个类的(包括其父类)所有方法

时间:2017-07-28 09:17:07      阅读:226      评论:0      收藏:0      [点我收藏+]

主函数:

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public class reflec {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
        TestClass r = new TestClass();
        Class<?> c1 = Class.forName(TestClass.class.getName());
        while (c1!=null) {
            for(Method m : c1.getDeclaredMethods()){
                System.out.println(Modifier.toString(m.getModifiers())+ " " +
m.getReturnType().getCanonicalName() +" "+m.getName()+ Arrays.toString(m.getParameters()) ); } c1=c1.getSuperclass(); System.out.println("----to super class----"); } } }

 

目标实体类:

技术分享
class TestClass{
    private int i;
    private int j;
    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
    }

    public int forSum(){
        return i+j;

    }
}
目标实体类

 

运行结果:

public int getI[]
public void setI[int arg0]
public int getJ[]
public void setJ[int arg0]
public int forSum[]
----to super class----
protected void finalize[]
public final void wait[long arg0, int arg1]
public final native void wait[long arg0]
public final void wait[]
public boolean equals[java.lang.Object arg0]
public java.lang.String toString[]
public native int hashCode[]
public final native java.lang.Class getClass[]
protected native java.lang.Object clone[]
private static native void registerNatives[]
public final native void notify[]
public final native void notifyAll[]
----to super class----

Process finished with exit code 0

通过反射来输出一个类的(包括其父类)所有方法

原文:http://www.cnblogs.com/kincolle/p/7248248.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!