补充代码,使用3种方法通过反射获取相应的对象。获取的class对象分别赋值给studentClass1,studentClass2,studentClass3。package step1; public class Reflection{ public static void main(String[] args) { Class <?> studentClass1 = null; Class <?> studentClass2 = null; Class <?> studentClass3 = null; // ---------------------Begin------------------------ // Studnet 类的路径为 "step1.Student" // ---------------------End------------------------ System.out.println(studentClass1); System.out.println(studentClass2); System.out.println(studentClass3); } }
时间: 2023-08-15 13:20:12 浏览: 78
package step1;
public class Reflection {
public static void main(String[] args) {
Class<?> studentClass1 = null;
Class<?> studentClass2 = null;
Class<?> studentClass3 = null;
// 方法一:通过类名.class获取Class对象
studentClass1 = Student.class;
// 方法二:通过对象.getClass()获取Class对象
Student student = new Student();
studentClass2 = student.getClass();
// 方法三:通过Class.forName获取Class对象
try {
studentClass3 = Class.forName("step1.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println(studentClass1);
System.out.println(studentClass2);
System.out.println(studentClass3);
}
}
阅读全文