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

📄 simpleclassviewer.java

📁 有关java学习技巧的课件
💻 JAVA
字号:


import java.lang.reflect.*;

public class SimpleClassViewer {
     public static void main(String[] args) { 
        try {
            Class c = Class.forName(args[0]);
            // 取得包代表对象
            Package p = c.getPackage();
            
            System.out.printf("package %s;%n", p.getName());
            
            // 取得类型修饰,像是class、interface
            int m = c.getModifiers();
            
            System.out.print(Modifier.toString(m) + " ");
            // 如果是接口
            if(Modifier.isInterface(m)) {
                System.out.print("interface ");
            }
            else {
                System.out.print("class ");
            }
            
            System.out.println(c.getName() + " {");

            // 取得声明的域成员代表对象
            Field[] fields = c.getDeclaredFields();
            for(Field field : fields) {
                // 显示权限修饰,像是public、protected、private
                System.out.print("\t" + 
                    Modifier.toString(field.getModifiers()));
                // 显示类型名称
                System.out.print(" " + 
                    field.getType().getName() + " ");
                // 显示域成员名称
                System.out.println(field.getName() + ";");
            }

            // 取得声明的构造函数代表对象            
            Constructor[] constructors = 
                            c.getDeclaredConstructors();
            for(Constructor constructor : constructors) {
                // 显示权限修饰,像是public、protected、private
                System.out.print("\t" + 
                     Modifier.toString(
                       constructor.getModifiers()));
                // 显示构造函数名称
                System.out.println(" " + 
                      constructor.getName() + "();");
            }
            // 取得声明的方法成员代表对象             
            Method[] methods = c.getDeclaredMethods();
            for(Method method : methods) {
                // 显示权限修饰,像是public、protected、private
                System.out.print("\t" + 
                     Modifier.toString(
                              method.getModifiers()));
                // 显示返回值类型名称
                System.out.print(" " + 
                     method.getReturnType().getName() + " ");
                // 显示方法名称
                System.out.println(method.getName() + "();");
            }
            System.out.println("}");
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("没有指定类");
        }
        catch(ClassNotFoundException e) {
            System.out.println("找不到指定类");
        }
    }
}

⌨️ 快捷键说明

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