JavaSE-24.2.3反射获取成员变量并使用
Posted yub4by
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSE-24.2.3反射获取成员变量并使用相关的知识,希望对你有一定的参考价值。
1 package day15.lesson2; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 7 /* 8 2.6 反射获取成员变量并使用 9 10 Class类获取成员变量对象的方法 11 Field[] getFields() 返回所有公共成员变量对象的数组 12 Field[] getDeclaredFields() 返回所有成员变量对象的数组 13 Field getField(String name) 返回单个公共成员变量对象 14 Field getDeclaredField(String name) 返回单个成员变量对象 15 16 Field类用于给成员变量赋值的方法 17 void set(Object obj,Object value) 给obj对象的成员变量赋值为value 18 19 */ 20 public class Demo4Reflect { 21 public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { 22 Class<?> c = Class.forName("day15.lesson2.Student"); 23 24 Field[] fields = c.getFields(); 25 for (Field f: fields){ 26 System.out.println(f); 27 } 28 //public java.lang.String day15.lesson2.Student.address 29 System.out.println(); 30 31 Field[] declaredFields = c.getDeclaredFields(); 32 for (Field f: declaredFields){ 33 System.out.println(f); 34 } 35 /* 36 private java.lang.String day15.lesson2.Student.name 37 int day15.lesson2.Student.age 38 public java.lang.String day15.lesson2.Student.address 39 */ 40 System.out.println(); 41 42 Field addressField = c.getField("address"); //公共的成员变量 43 /*Student s = new Student(); 44 s.address = "南京"; 45 System.out.println(s);*/ 46 //通过反射实现上述功能 47 Constructor<?> con = c.getConstructor(); //获取公共无参构造 48 Object obj = con.newInstance(); //创建对象 49 addressField.set(obj, "南京"); //给对象obj的成员变量addressField赋值"南京" 50 System.out.println(obj); //Student{name=\'null\', age=0, address=\'南京\'} 51 System.out.println(); 52 53 // Field declaredField = c.getDeclaredField("name"); //私有成员变量 54 } 55 }
1 class Student{ 2 //成员变量 3 private String name; //私有 4 int age; //默认 5 public String address; //公共 6 7 //构造方法 8 public Student() { //公共 9 } 10 11 public Student(String name, int age, String address) { //公共 12 this.name = name; 13 this.age = age; 14 this.address = address; 15 } 16 17 private Student(String name){ //私有 18 this.name = name; 19 } 20 21 Student(String name, int age){ //默认 22 this.name = name; 23 this.age = age; 24 } 25 26 //成员方法 27 private void function(){ //私有 28 System.out.println("function"); 29 } 30 31 public void method1(){ //公共 32 System.out.println("method"); 33 } 34 35 public void method2(String s){ //公共 36 System.out.println("method:" + s); 37 } 38 39 public String method3(String s, int i){ //公共 40 return s + "," + i; 41 } 42 43 @Override 44 public String toString() { //公共 45 return "Student{" + 46 "name=\'" + name + \'\\\'\' + 47 ", age=" + age + 48 ", address=\'" + address + \'\\\'\' + 49 \'}\'; 50 } 51 }
1 package day15.lesson2; 2 3 import java.lang.reflect.Constructor; 4 import java.lang.reflect.Field; 5 import java.lang.reflect.InvocationTargetException; 6 7 /* 8 2.7 反射获取成员变量并使用练习 9 10 通过反射获取成员变量并赋值 11 12 通过反射实现一下操作: 13 Student s = new Studet(); 14 s.name = "tom"; 15 s.age = 23; 16 s.address = "南京"; 17 sout(s); 18 */ 19 public class Demo5Reflect { 20 public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { 21 Class<?> c = Class.forName("day15.lesson2.Student"); 22 23 // Student s = new Student(); 24 Constructor<?> con = c.getConstructor(); 25 Object obj = con.newInstance(); 26 System.out.println(obj); //Student{name=\'null\', age=0, address=\'null\'} 27 28 // s.name = "tom"; 29 Field nameField = c.getDeclaredField("name"); //私有只能用xxxDeclaredxxx 30 nameField.setAccessible(true); //私有得暴力反射 31 nameField.set(obj, "tom"); 32 System.out.println(obj); //Student{name=\'tom\', age=0, address=\'null\'} 33 34 // s.age = 23; 35 Field ageField = c.getDeclaredField("age"); //默认的也只能用xxxDeclaredxxx 36 ageField.set(obj, 23); 37 System.out.println(obj); //Student{name=\'tom\', age=23, address=\'null\'} 38 39 // s.address = "南京"; 40 // Field addressField = c.getField("address"); //公共的既能用getField也能用getDeclaredField 41 Field addressField = c.getDeclaredField("address"); //为了方便统一用getDeclaredField即可 42 addressField.set(obj, "南京"); 43 System.out.println(obj); //Student{name=\'tom\', age=23, address=\'南京\'} 44 } 45 }
以上是关于JavaSE-24.2.3反射获取成员变量并使用的主要内容,如果未能解决你的问题,请参考以下文章