1 package com.part1; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.Method; 7 8 public class Test1 { 9 public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException { 10 //1.获取Class对象三种方式 11 //1.1 getClass方法反射只能获取类信息(比较烦) 12 Student stu=new Student(); 13 Class cla=stu.getClass(); 14 System.out.println(cla); 15 16 //1.2 class方法反射获取类或接口信息 (更方便) 17 Class cla2=Student.class; 18 System.out.println(cla2); 19 20 //1.3 forName方法根据地址反射获取类或接口信息(更精确) 21 Class cla3=Class.forName("com.part1.Student"); 22 System.out.println(cla3); 23 24 //2.动态创建对象 25 //2.1 newInstance创建对象(无参数) 26 Student stu2=(Student) cla2.newInstance(); 27 System.out.println(stu2); 28 29 //2.2通过有参数构造创建对象 30 //获取有参数的构造方法 31 Constructor con=cla3.getConstructor(int.class,String.class,int.class); 32 33 //根据有参数的构造方法创建对象 34 Student stu3=(Student) con.newInstance(1,"holly",18); 35 System.out.println(stu3); 36 37 //3.获取对象的属性 38 //3.1 获取public字段 39 System.out.println("-----------------获取public字段----------------"); 40 //3.1.1 只能获取所有公用的属性 41 Field[] stuFields=cla2.getFields(); 42 for (Field i : stuFields) { 43 System.out.println(i); 44 } 45 //3.1.2 获取某一个公共的属性 46 Field sexField=cla2.getField("sex"); 47 48 //3.1.3 给某个公共属性赋值 49 sexField.set(stu2, "女"); 50 51 //3.1.4 获取某个公共属性值 52 System.out.println(sexField.get(stu2)); 53 54 //3.2获取私有的字段 55 System.out.println("-----------获取字段和访问修饰符无关----------"); 56 //3.2.1 获取所有字段和访问修饰符无关 57 Field[] fields3=cla3.getDeclaredFields(); 58 for (Field field : fields3) { 59 System.out.println(field); 60 } 61 62 //3.2.2 获取某个私有字段 63 Field snameField=cla3.getDeclaredField("sname"); 64 65 //3.2.3 给某个私有字段解除权限,参数为true截取权限限制 66 snameField.setAccessible(true); 67 68 //3.2.4 给解除的私有字段赋值 69 snameField.set(stu3, "王静娴"); 70 71 //3.2.5 获取某个字段的属性值 72 System.out.println(snameField.get(stu3)); 73 74 //4.获取对象的方法 75 System.out.println("----获取所有的公共方法----"); 76 //4.1 获取公有方法 77 //4.1.1 获取所有的公共的方法 78 Method[] methods =cla2.getMethods(); 79 for (Method method : methods) { 80 System.out.println(method); 81 } 82 83 //4.1.2 获取某个公共的方法 84 Method passwordMethod=cla2.getMethod("setPassword", int.class); 85 86 //4.1.3 动态调用方法 87 passwordMethod.invoke(stu2, 123); 88 System.out.println(stu2); 89 90 //4.2 获取方法(和访问修饰符无关) 91 //4.2.1 获取所有的方法和访问修饰符无关 92 Method[] methods2=cla3.getDeclaredMethods(); 93 for (Method method : methods2) { 94 System.out.println(method); 95 } 96 97 //4.2.2 获取某个私有方法,获取的方法签名 98 Method eatMethod=cla3.getDeclaredMethod("eat", String.class); 99 100 //4.2.3 解除方法操作权限 101 eatMethod.setAccessible(true); 102 103 //4.2.4 调用某个方法 104 eatMethod.invoke(stu3, "中午吃木桶饭"); 105 106 107 } 108 109 }
1、创建Student.java
1 package com.part1; 2 /** 3 * 学生类 4 * @author pc 5 * 6 */ 7 public class Student { 8 private int sid; 9 private String sname; 10 private int password; 11 private int age; 12 public String sex; 13 14 public Student() { 15 } 16 17 public Student(int sid, String sname, int password) { 18 this.sid = sid; 19 this.sname = sname; 20 this.password = password; 21 } 22 23 public Student(int sid, String sname, int password, int age, String sex) { 24 this.sid = sid; 25 this.sname = sname; 26 this.password = password; 27 this.age = age; 28 this.sex = sex; 29 } 30 31 public int getSid() { 32 return sid; 33 } 34 public void setSid(int sid) { 35 this.sid = sid; 36 } 37 public String getSname() { 38 return sname; 39 } 40 public void setSname(String sname) { 41 this.sname = sname; 42 } 43 public int getPassword() { 44 return password; 45 } 46 public void setPassword(int password) { 47 this.password = password; 48 } 49 public int getAge() { 50 return age; 51 } 52 public void setAge(int age) { 53 this.age = age; 54 } 55 public String getSex() { 56 return sex; 57 } 58 public void setSex(String sex) { 59 this.sex = sex; 60 } 61 62 private void eat(String food){ 63 System.out.println(food); 64 } 65 @Override 66 public String toString() { 67 return "Student [age=" + age + ", password=" + password + ", sex=" 68 + sex + ", sid=" + sid + ", sname=" + sname + "]"; 69 } 70 71 72 73 }
2、创建测试类Test.java
1 package com.part1; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.Method; 7 8 public class Test1 { 9 public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException { 10 //1.获取Class对象三种方式 11 //1.1 getClass方法反射只能获取类信息(比较烦) 12 Student stu=new Student(); 13 Class cla=stu.getClass(); 14 System.out.println(cla); 15 16 //1.2 class方法反射获取类或接口信息 (更方便) 17 Class cla2=Student.class; 18 System.out.println(cla2); 19 20 //1.3 forName方法根据地址反射获取类或接口信息(更精确) 21 Class cla3=Class.forName("com.part1.Student"); 22 System.out.println(cla3); 23 24 //2.动态创建对象 25 //2.1 newInstance创建对象(无参数) 26 Student stu2=(Student) cla2.newInstance(); 27 System.out.println(stu2); 28 29 //2.2通过有参数构造创建对象 30 //获取有参数的构造方法 31 Constructor con=cla3.getConstructor(int.class,String.class,int.class); 32 33 //根据有参数的构造方法创建对象 34 Student stu3=(Student) con.newInstance(1,"holly",18); 35 System.out.println(stu3); 36 37 //3.获取对象的属性 38 //3.1 获取public字段 39 System.out.println("-----------------获取public字段----------------"); 40 //3.1.1 只能获取所有公用的属性 41 Field[] stuFields=cla2.getFields(); 42 for (Field i : stuFields) { 43 System.out.println(i); 44 } 45 //3.1.2 获取某一个公共的属性 46 Field sexField=cla2.getField("sex"); 47 48 //3.1.3 给某个公共属性赋值 49 sexField.set(stu2, "女"); 50 51 //3.1.4 获取某个公共属性值 52 System.out.println(sexField.get(stu2)); 53 54 //3.2获取私有的字段 55 System.out.println("-----------获取字段和访问修饰符无关----------"); 56 //3.2.1 获取所有字段和访问修饰符无关 57 Field[] fields3=cla3.getDeclaredFields(); 58 for (Field field : fields3) { 59 System.out.println(field); 60 } 61 62 //3.2.2 获取某个私有字段 63 Field snameField=cla3.getDeclaredField("sname"); 64 65 //3.2.3 给某个私有字段解除权限,参数为true截取权限限制 66 snameField.setAccessible(true); 67 68 //3.2.4 给解除的私有字段赋值 69 snameField.set(stu3, "王静娴"); 70 71 //3.2.5 获取某个字段的属性值 72 System.out.println(snameField.get(stu3)); 73 74 //4.获取对象的方法 75 System.out.println("----获取所有的公共方法----"); 76 //4.1 获取公有方法 77 //4.1.1 获取所有的公共的方法 78 Method[] methods =cla2.getMethods(); 79 for (Method method : methods) { 80 System.out.println(method); 81 } 82 83 //4.1.2 获取某个公共的方法 84 Method passwordMethod=cla2.getMethod("setPassword", int.class); 85 86 //4.1.3 动态调用方法 87 passwordMethod.invoke(stu2, 123); 88 System.out.println(stu2); 89 90 //4.2 获取方法(和访问修饰符无关) 91 //4.2.1 获取所有的方法和访问修饰符无关 92 Method[] methods2=cla3.getDeclaredMethods(); 93 for (Method method : methods2) { 94 System.out.println(method); 95 } 96 97 //4.2.2 获取某个私有方法,获取的方法签名 98 Method eatMethod=cla3.getDeclaredMethod("eat", String.class); 99 100 //4.2.3 解除方法操作权限 101 eatMethod.setAccessible(true); 102 103 //4.2.4 调用某个方法 104 eatMethod.invoke(stu3, "中午吃木桶饭"); 105 } 106 107 }
3、运行效果
1 class com.part1.Student 2 class com.part1.Student 3 class com.part1.Student 4 Student [age=0, password=0, sex=null, sid=0, sname=null] 5 Student [age=0, password=18, sex=null, sid=1, sname=holly] 6 -----------------获取public字段---------------- 7 public java.lang.String com.part1.Student.sex 8 女 9 -----------获取字段和访问修饰符无关---------- 10 private int com.part1.Student.sid 11 private java.lang.String com.part1.Student.sname 12 private int com.part1.Student.password 13 private int com.part1.Student.age 14 public java.lang.String com.part1.Student.sex 15 王静娴 16 ----获取所有的公共方法---- 17 public java.lang.String com.part1.Student.toString() 18 public void com.part1.Student.setPassword(int) 19 public int com.part1.Student.getSid() 20 public void com.part1.Student.setSid(int) 21 public java.lang.String com.part1.Student.getSname() 22 public void com.part1.Student.setSname(java.lang.String) 23 public int com.part1.Student.getPassword() 24 public int com.part1.Student.getAge() 25 public void com.part1.Student.setAge(int) 26 public java.lang.String com.part1.Student.getSex() 27 public void com.part1.Student.setSex(java.lang.String) 28 public final void java.lang.Object.wait() throws java.lang.InterruptedException 29 public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException 30 public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException 31 public native int java.lang.Object.hashCode() 32 public final native java.lang.Class java.lang.Object.getClass() 33 public boolean java.lang.Object.equals(java.lang.Object) 34 public final native void java.lang.Object.notify() 35 public final native void java.lang.Object.notifyAll() 36 Student [age=0, password=123, sex=女, sid=0, sname=null] 37 public java.lang.String com.part1.Student.toString() 38 public void com.part1.Student.setPassword(int) 39 private void com.part1.Student.eat(java.lang.String) 40 public int com.part1.Student.getSid() 41 public void com.part1.Student.setSid(int) 42 public java.lang.String com.part1.Student.getSname() 43 public void com.part1.Student.setSname(java.lang.String) 44 public int com.part1.Student.getPassword() 45 public int com.part1.Student.getAge() 46 public void com.part1.Student.setAge(int) 47 public java.lang.String com.part1.Student.getSex() 48 public void com.part1.Student.setSex(java.lang.String) 49 中午吃木桶饭