⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reflectiontest.java

📁 贪食蛇的实现源码
💻 JAVA
字号:


import java.lang.reflect.*;

public class ReflectionTest
{
    public static void main(String[] args){
       if(args.length == 0 ){
           System.out.println("Usage: java sample.ReflectionTest class_name");
           System.exit(-1);
       }

       String name = args[0];

      try{  
         Class cl = Class.forName(name);  // get the class object of a given class name
         Class supercl = cl.getSuperclass();  // get parent class object
         System.out.print("class " + name);   // print class name
         if (supercl != null && !supercl.equals(Object.class))
             System.out.print(" extends " + supercl.getName());  // print parent class name except Object 
         System.out.print("\n{\n");  // change  line, print {  , then change line again

         printConstructors(cl);   // print all constructors
         System.out.println();

         printMethods(cl);        // print all methods
         System.out.println();

         printFields(cl);        // print all  attributes
         System.out.println("}");    //print }
      }
      catch(ClassNotFoundException e){ 
              System.out.println(" Sorry, your Class can not be found.");
      }
   }


   public static void printConstructors(Class cl) {  
      Constructor[] constructors = cl.getDeclaredConstructors();

      for (int i = 0; i < constructors.length; i++){ 
         Constructor c = constructors[i]; 
         String name = c.getName();                              // get the constructor name
         System.out.print(Modifier.toString(c.getModifiers()));  // print constructor modifer by using static method
         System.out.print( " " + name + "("  );                  //  print constructor name and (

         Class[] paramTypes = c.getParameterTypes();             // get all Class objects of ParameterTypes
         for (int j = 0; j < paramTypes.length; j++)
         {  
            if (j > 0) System.out.print( ", " );                 // print ,
            System.out.print(paramTypes[j].getName());           // print parameter type
         }
         System.out.println( ");" );                              // print ););
      }
   }

   public static void printMethods(Class cl) { 
      Method[] methods = cl.getDeclaredMethods();

      for (int i = 0; i < methods.length; i++)
      {  
         Method m = methods[i];
         Class retType = m.getReturnType();        
         String name = m.getName();
         System.out.print(Modifier.toString(m.getModifiers()));    // print method modifer
         System.out.print(" " + retType.getName() + " " + name + "(");  //  print method name and (

	 Class[] paramTypes = m.getParameterTypes();
         for (int j = 0; j < paramTypes.length; j++)
         {
           if (j > 0) 
                 System.out.print(", ");  // print ,

            System.out.print(paramTypes[j].getName());
         }
         System.out.println(  ");" );                                 // print );
      }
   }

   public static void printFields(Class cl)
   {  
      Field[] fields = cl.getDeclaredFields();

      for (int i = 0; i < fields.length; i++)
      {  
         Field f = fields[i];
         Class type = f.getType();
         String name = f.getName();
         System.out.print(Modifier.toString(f.getModifiers()));     // print attribute modifer
         System.out.println(" " + type.getName() + " " + name + ";" );     //  print attribute name and ;
 
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -