zoukankan      html  css  js  c++  java
  • 通过反射来输出一个类的(包括其父类)所有方法

    主函数:

    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

  • 相关阅读:
    数组迭代方法
    promise
    Gulp执行预处理
    第一个gulp 项目
    vue 单元素过渡
    webpack 入门
    webpack初始化
    v-for 指令
    ajax 工作原理
    面试小问题
  • 原文地址:https://www.cnblogs.com/kincolle/p/7248248.html
Copyright © 2011-2022 走看看