获取Class类对象的三种方式

三种方式分类

  • 类名.class属性

  • 对象名.getClass()方法

  • Class.forName(全类名)方法

示例代码

public class ReflectDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        //使用类的class属性来获取该类对应的Class对象
        Class c1 = Student.class;
        System.out.println(c1);

        Class c2 = Student.class;
        System.out.println(c1 == c2);
        System.out.println("--------");

        //调用对象的getClass()方法,返回该对象所属类对应的Class对象
        Student s = new Student();
        Class c3 = s.getClass();
        System.out.println(c1 == c3);
        System.out.println("--------");

        //使用Class类中的静态方法forName(String className)
        Class c4 = Class.forName("com.leon_02.Student");
        System.out.println(c1 == c4);
    }
}

 

你可能感兴趣的:(获取Class类对象的三种方式)