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例源代码]。

原文地址:https://www.cnblogs.com/zj0208/p/5924038.html