獲取字節(jié)碼的方式如下:
1. 通過(guò)new對(duì)象實(shí)現(xiàn)反射機(jī)制;
2. 通過(guò)路徑實(shí)現(xiàn)反射機(jī)制;
3. 通過(guò)類名實(shí)現(xiàn)反射機(jī)制。
示例代碼: public class Student { private int id; String name; protected boolean sex; public float score; } public class Get { //獲取反射機(jī)制三種方式 public static void main(String[] args) throws ClassNotFoundException {
//方式一(通過(guò)建立對(duì)象) Student stu = new Student(); Class classobj1 = stu.getClass(); System.out.println(classobj1.getName());
//方式二(所在通過(guò)路徑-相對(duì)路徑) Class classobj2 = Class.forName("fanshe.Student"); System.out.println(classobj2.getName());
//方式三(通過(guò)類名) Class classobj3 = Student.class; System.out.println(classobj3.getName()); } }