public class JavaTest {
public static void main(String[] args) {
// 方式一:通过getClass()方法获取
People people = new People();
Class c1 = people.getClass();
System.out.println(c1);
// 方式二:通过内置class属性
Class c2 = People.class;
System.out.println(c2);
// 方式三:调用class类提供的静态方法forName
try {
Class c3 = Class.forName("com.miao.test01.People");
System.out.println(c3);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 方式四:利用类加载器
try {
//获取类加载器
ClassLoader loader = JavaTest.class.getClassLoader();
Class c4 = loader.loadClass("com.miao.test01.People");
System.out.println(c4);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}