获取类的有关信息

    技术2025-11-20  14

    import java.lang.reflect.*;

     

    class Rect

    {

    double width,height,area;

    public double getArea()

    {

      area=width*height;

      return area;

    }

    }

    public class hello {

    public static void main(String[] args) 

    {

    // TODO Auto-generated method stub

    Rect rect=new Rect();

    Class cs=rect.getClass();

    String classname=cs.getName();

    Constructor[] cons=cs.getDeclaredConstructors();

    Field[] fields=cs.getDeclaredFields();

    Method[] methods=cs.getDeclaredMethods();

    System.out.println("类名是:"+classname);

    System.out.println("/n构造函数是:");

    for(int i=0;i<cons.length;i++)

    {

    System.out.println("/n"+cons[i].toString());

    }

    System.out.println("/n成员变量:");

    for(int i=0;i<fields.length;i++)

    {

    System.out.println("/n"+fields[i].toString());

    }

    System.out.println("/n方法:");

    for(int i=0;i<methods.length;i++)

    {

    System.out.println("/n"+methods[i].toString());

    }

    }

    }

    最新回复(0)