码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:style   private   alt   except   class   new   bsp   chm   ram   

主函数:

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

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

标签:style   private   alt   except   class   new   bsp   chm   ram   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!