方法一:
使用getClass()方法获取Class对象
public class Demo1 {
public static void main(String[] args){
//创建一个Student类的对象
Student1 student1 = new Student1();
//通过getClass()方法获取Class对象
Class c1 = student1.getClass();
System.out.println(c1);
}
}
/**
* 自定义学生类
*/
class Student1{
}
方法二:
通过Student.class获取Class对象
public class Demo1 {
public static void main(String[] args){
//创建一个Student类的对象
Student1 student1 = new Student1();
//通过Student1.class获取Class对象
Class c2 = Student1.class;
System.out.println(c2);
}
}
/**
* 自定义学生类
*/
class Student1{
}
方法三:
通过forName()获取Class对象
public class Demo1 {
public static void main(String[] args){
//创建一个Student类的对象
Student1 student1 = new Student1();
//通过Class类提供的静态方法forName()获取Class对象
try {
Class c3 = Class.forName("www.jin.Student1");
System.out.println(c3);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
/**
* 自定义学生类
*/
class Student1{
}