import java.lang.reflect.*;
class Person 
{
   private String name;
   private int age;
   public Person(String name,int age)
   {
        this.name=name;
        this.age=age;
   }
   public Person()
   {
        name="zhangsan";
        age=20;
   }
   public String toString()
   {
          return "name="+" "+name+" "+"age="+" "+age;
   }
}class Student extends Person
{
   private String school;
   public Student(String name,int age,String school)
   {
         super(name,age);
         this.school=school;
   }
   public Student()
   {
         super();
         school="liaoningshiyou";
   }
   public static void main(String[]args)
   {
         try
         {
               String name=null;
         if(args.length>0) name=args[0];
         Class c1=Class.forName(name);
         Class c2=c1.getSuperclass();
         System.out.print("class"+" "+name);
         if(c2!=null&&!c2.equals(Object.class)) 
         System.out.print(" "+"extends"+" "+c2.getName());
         System.out.println();
         System.out.println("{");
         System.out.println();
         printConstructor(c1);
         printMethod(c1);
         printField(c1);
         System.out.println("}");          
           }
         catch(Exception e)
         {
               e.printStackTrace();
         }
   }
   public static void printConstructor(Class c)
   {
         Constructor []constructors=c.getDeclaredConstructors();         
         for(int i=0;i<constructors.length;i++)
         {
               Constructor cs=constructors[i];
               Class[]cl=cs.getParameterTypes();
               System.out.print(Modifier.toString(cs.getModifiers())+" ");
               System.out.print(cs.getName()+"(");
               for(int j=0;j<cl.length;j++)
               {
                    if(j>0)System.out.print(",");
                    else if(j==0)System.out.print(" ");
                    System.out.print(cl[j].getName());
               }
               System.out.print(")");
               System.out.println();                
         }          
   }
   public static void printMethod(Class c)
   {
         Method []methods=c.getDeclaredMethods();
         for(int i=0;i<methods.length;i++)
         {
               Method m=methods[i];
               Class[]cl=m.getParameterTypes();
               System.out.print(Modifier.toString(m.getModifiers())+" ");
               System.out.print(m.getReturnType().getName()+" ");
               System.out.print(m.getName()+"(");
               for(int j=0;j<methods.length;j++)
               {
                     if(j>0) System.out.print(",");
                     else if(j==0)System.out.print(" ");
                     else System.out.print(cl[j].getName());
               }
               System.out.print(")");
               System.out.println();
         }            
   }
   public static void printField(Class c)
   {
         Field[]fields=c.getDeclaredFields();
         for(int i=0;i<fields.length;i++)
         {
               Field f=fields[i];
               System.out.print(Modifier.toString(f.getModifiers())+" ");
               System.out.print(f.getType().getName()+" ");
               System.out.print(f.getName());
               System.out.println();
         }          
   }
}
输出结果中有一行是static java.long.Class class$<.....> 请问这是什么啊

解决方案 »

  1.   

    java.lang.Class 1.0Field[] getFields() 1.1Field[] getDeclaredFields() 1.1The getFields method returns an array containing Field objects for the public fields of this class or its superclasses. The geTDeclaredField method returns an array of Field objects for all fields of this class. The methods return an array of length 0 if there are no such fields or if the Class object represents a primitive or array type.Method[] getMethods() 1.1Method[] getDeclaredMethods() 1.1return an array containing Method objects: getMethods returns public methods and includes inherited methods; getdeclaredMethods returns all methods of this class or interface but does not include inherited methods.Constructor[] getConstructors() 1.1Constructor[] getDeclaredConstructors() 1.1return an array containing Constructor objects that give you all the public constructors (for getConstructors) or all constructors (for getdeclaredConstructors) of the class represented by this Class object.简单点说:一个类一般都有字段域(Field), 方法(Method), 构造函数(Constructor), Class类反映了一个类的基本信息,通过Class类的相关方法(getDeclaredMethods() ,getDeclaredFields(),getDeclaredConstructors() )可以获得一个类的字段域(Field), 方法(Method), 构造函数(Constructor)!
    你的程序就是输入一个类名, 程序将输出这个类的有关字段域、方法、构造函数!
      

  2.   

    没问题啊
    输入:java.lang.Class
    输出:
    class java.lang.Class
    {private java.lang.Class()
    public native boolean isInstance( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    public native int getModifiers( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    private java.lang.Object newInstance0( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    public static java.lang.Class forName( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    public static java.lang.Class forName( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    private static native java.lang.Class forName0( ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)
    public native java.lang.String getName

    private static sun.reflect.ReflectionFactory reflectionFactory
    private static boolean initted
    static java.lang.Class class$java$lang$Class
    }太长了 中间省略了
      

  3.   

    static java.lang.Class class$java$lang$Class
    这个是字段吗
    怎么会出现这个呢