1. 程式人生 > 實用技巧 >資料結構-01:線性表

資料結構-01:線性表

獲取Class 的四種方式

public static void testReflect() throws Exception {
        //直接使用物件的getClass()方法,getClass()方法是Object物件的方法
        Person p1 = new Person();
        Class clazz1 = p1.getClass();
        //直接。class獲取
        Class clazz2 = Person.class;
        //classForName()方法獲取
        Class clazz3 = Class.
forName("com.mystudy.Person"); //ClassLoad獲取 ClassLoader classLoader= MyTest.class.getClassLoader(); Class clazz4= classLoader.loadClass("com.mystudy.Person"); System.out.println(clazz1==clazz2); System.out.println(clazz1==clazz3); System.
out.println(clazz3==clazz2); System.out.println(clazz1==clazz4); }

在這裡插入圖片描述
總結:獲取到的是同一個物件,所有具體類的例項的Class 只有一個