首页 > 应用开发 > Java > JAVA基础学习篇之反射
这块代码如果将class文件删除后,第一次运行成功,第二次运行也许会成功也许会报错,这是因为内存中还有class文件。多运行几次就会报错 找不到class文件
import cn.chen.pojo.Student;
import java.io.File;
public class Test {
public static void main(String[] args) {
Student student=new Student();
File file=new File("E:\\java\\Reflection\\out\\production\\Reflection\\cn\\chen\\pojo\\Student.class");
if (file.exists()){
file.delete();
}
Class aClass = student.getClass();
System.out.println(aClass);
}
}
2.获取class对象内容信息
2.1.获取Class对象的三种方式 郑州看尖锐湿疣哪家医院好 http://www.zykdmnk.com/
package cn.chen.demo;
import cn.chen.pojo.Student;
//获取Class对象的三种方式
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException {
//第一种直接.class获取
Class studentClass = Student.class;
System.out.println(studentClass);
//第二种通过Object类中的getClass方法获取
Student student=new Student();
Class aClass = student.getClass();
System.out.println(aClass);
//第三种通过Class.forName("全限定类名")方法获取
Class aClass1 = Class.forName("cn.chen.pojo.Student");
System.out.println(aClass1);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69979119/viewspace-2715530/ ,如需转载,请注明出处,否则将追究法律责任。