• Class对象


    (一) 获得Class对象的四种方式

    • 第1种方法:Object.getClass()
    • 第2种方法:.class语法
    • 第3种方法:Class.forName()
    • 第4种方法:包装类的TYPE域
    import java.util.Date;
    
    public class Demo1 {
    
        public static void main(String[] args) throws ClassNotFoundException {
    
            // 第1种方法:Object.getClass()
            Class c1 = new Date().getClass();// 使用getClass()方式获得Class对象
            System.out.println(c1.getName());// 输出对象名称
    
            // 第2种方法:.class语法
            Class c2 = boolean.class;// 使用.class语法获得Class对象
            System.out.println(c2.getName());// 输出对象名称
    
            // 第3种方法:Class.forName()
            Class c3 = Class.forName("java.lang.String");// 使用Class.forName()获得Class对象
            System.out.println(c3.getName());// 输出对象名称
    
            // 第4种方法:包装类的TYPE域
            Class c4 = Double.TYPE;// 使用包装类获得Class对象
            System.out.println(c4.getName());// 输出对象名称
        }
    
    }

    (二) 获取类对象的

    • 类的标准名称
    • 类的修饰符
    • 类的泛型参数
    • 类所实现的接口
    • 类的直接继承类
    • 类的注解
    • 类的构造方法
    • 类的非继承域变量
    • 类的非继承方法
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.lang.reflect.Modifier;
    import java.lang.reflect.Type;
    import java.lang.reflect.TypeVariable;
    
    public class Demo2 {
    
        public static void main(String[] args) throws ClassNotFoundException {
    
            // 获得ArrayList类对象
            Class<?> clazz = Class.forName("java.util.ArrayList");
    
            // 类的标准名称
            System.out.println("类的标准名称:" + clazz.getCanonicalName());
    
            // 类的修饰符
            System.out.println("类的修饰符:" + Modifier.toString(clazz.getModifiers()));
    
            // 输出类的泛型参数
            System.out.print("类的泛型参数:");
            TypeVariable<?>[] typeVariables = clazz.getTypeParameters();
            if (typeVariables.length != 0) {
                for (TypeVariable<?> typeVariable : typeVariables) {
                    System.out.println(typeVariable + "	");
                }
            } else {
                System.out.println("空");
            }
    
            // 输出类所实现的所有接口
            System.out.println("类所实现的接口:");
            Type[] interfaces = clazz.getGenericInterfaces();
            if (interfaces.length != 0) {
                for (Type type : interfaces) {
                    System.out.println("	" + type);
                }
            } else {
                System.out.println("	" + "空");
            }
    
            // 输出类的直接继承类,如果是继承自Object则返回空
            System.out.print("类的直接继承类:");
            Type superClass = clazz.getGenericSuperclass();
            if (superClass != null) {
                System.out.println(superClass);
            } else {
                System.out.println("空");
            }
    
            // 输出类的所有注释信息,有些注释信息是不能用反射获得的
            System.out.print("类的注解:");
            Annotation[] annotations = clazz.getAnnotations();
            if (annotations.length != 0) {
                for (Annotation annotation : annotations) {
                    System.out.println("	" + annotation);
                }
            } else {
                System.out.println("空");
            }
    
            // 获得该类对象的所有构造方法
            System.out.println("类的构造方法:");
            Constructor<?>[] constructors = clazz.getConstructors();
            if (constructors.length != 0) {
                for (Constructor<?> constructor : constructors) {
                    System.out.println("	" + constructor);// 输出构造方法
                }
            } else {
                System.out.println("	空");
            }
    
            // 获得该类对象的所有非继承域
            System.out.println("类的非继承域变量:");
            Field[] fields = clazz.getDeclaredFields();
    
            if (fields.length != 0) {
                for (Field field : fields) {
                    System.out.println("	" + field);// 输出非继承域
                }
            } else {
                System.out.println("	空");
            }
    
            // 获得该类对象的所有非继承方法
            System.out.println("类的非继承方法:");
            Method[] methods = clazz.getDeclaredMethods();
            if (methods.length != 0) {
                for (Method method : methods) {
                    System.out.println(method);// 输出非继承方法
                }
            } else {
                System.out.println("	空");
            }
        }
    
    }

    代码来源:明日科技[Java经典编程300例源代码]。

  • 相关阅读:
    tp5使用jwt生成token,做api的用户认证
    thinkphp5.0多条件模糊查询以及多条件查询带分页如何保留参数
    tp5.1 模型 where多条件查询 like 查询 --多条件查询坑啊!!(tp5.1与tp5.0初始化控制器不一样)
    获取客户端IP地址-----以及--------线上开启redis扩展
    分享几个免费IP地址查询接口(API)
    thinkphp5选择redis库,让数据存入不同的redis库
    【JZOJ4824】【NOIP2016提高A组集训第1场10.29】配对游戏
    【JZOJ1637】【ZJOI2009】狼和羊的故事
    【JZOJ1611】Dining
    【JZOJ2224】【NOI2006】最大获利
  • 原文地址:https://www.cnblogs.com/zj0208/p/5924038.html
Copyright © 2020-2023  润新知