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

主函数:

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

时间: 2024-08-24 10:04:26

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

利用反射api查找一个类的详细信息

说到这个实例,首先介绍下本人,我是一个php程序员,从事drupal开发2年多,可以说从实习开始就接触这个,至今没有换过,drupal给我的感觉是俩字"强大",今天写一个views的字段,然后需要继承views的views_handler_field类,还要自己实现里面的一些方法,走一些自己的配置设置,查看这个类的时候,发现实在是太多信息了,并且做了好些继承,于是我就想要是能实现一个功能,传入一个类名,然后就能返回类的所有信息(包括,属性,方法,继承,接口,并且这些类所放置的文件位置,

C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值

转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的实例化的对象tc, 遍历获取所有属性(子成员)的方法(采用反射): Type t = tc.GetType();//获得该类的Type //再用Type.GetProperties获得PropertyInfo[],然后就可以用foreach 遍历了 foreach (PropertyInfo pi

C#判断一个类中有无&quot;指定名称&quot;的方法

C#中可以通过反射分析元数据来解决这个问题,示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 using System; using System.Reflection; namespace Hello {     class Program     {  

数据抓取的一个类,包含一些常用的方法

原文:数据抓取的一个类,包含一些常用的方法 using System;using System.Configuration;using System.IO;using System.Net;using System.Text;using System.Text.RegularExpressions; namespace XXX{    /// <summary>    /// Func 的摘要说明.    /// </summary>    public class Func   

C++用重载输出一个类

还记得刚开始学习C++时候,对于 cout 可以直接输出各种类型的变量很好奇. 毕竟是大一刚学完C语言,知道如果要输出什么数据肯定是要指定类型输出的 printf(). 对于C++中的一个变量 string str,使用 cout 可以直接输出string, 然而采用C中使用的 printf("%s", str) 是绝对无法输出该变量的.因为 这里的 str 是 一个 class.而不是一个 char* 类型的变量.当然,如果非要用printf()输出 str 也不是 不可以.采用 p

【转】重复输出一个给定的字符串的几种方法

方法1:通过 `while` 循环重复输出一个字符串 解题思路:while 语句只要指定的条件计算结果为true的时候,就执行其语句.while 语句的语法是这样的: 1 while (expression) 2 statement 在每次通过循环之前计算条件结果.如果条件为true,则执行语句.如果条件为false,则执行继续 while 循环之后的任何语句. 只要条件为true,语句就会执行. 这里是解决方案: function repeatString(str, times) { //空字

java反射1--产生Class类的实例化对象三个方法

首先每一个类在实例化的时候都会产生一个.class文件.而Class对象既是将.class文件读入内存时,为它创建一个Class对象. 反射就是对Class对象进行操作. 1 package reflect.vo; 2 3 /** 4 * @author guohao 5 * java测试类Student 6 */ 7 public class Student { 8 private String name; 9 private int age; 10 private float score;

C#.net利用反射,遍历获得一个类的所有属性名,方法名,成员名

public void PrintInstanceInfor(object t)  {        //获取所有方法      System.Reflection.MethodInfo[] methods = t.GetMethods();      //获取所有成员      System.Reflection.MemberInfo[] members = t.GetMembers();           //获取所有属性      System.Reflection.PropertyIn

java利用反射来调用一个类的私有方法

public class Calculator2{ private int add(int a,int b){ return a+b; } } public class Test { public static void main(String[] args){ Calculator2 calculator2 = new Calculator2(); Class<Calculator2> clazz = Calculator2.class; Object result = null; try{