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

主函数:

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

转载于:https://www.cnblogs.com/kincolle/p/7248248.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值