今天的Java学习将上节课所学习到的方法进行了测试。
1、获取成员变量,其中有方法比较特殊,我直接写在了注释中
1 //辅类 2 3 public class Person { 4 private String name; 5 private int age; 6 public String a; 7 protected String b; 8 String c; 9 private String d; 10 11 public Person(String name, int age) { 12 this.name = name; 13 this.age = age; 14 } 15 public Person(){} 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public int getAge() { 26 return age; 27 } 28 29 public void setAge(int age) { 30 this.age = age; 31 } 32 33 @Override 34 public String toString() { 35 return "Person{" + 36 "name='" + name + '\'' + 37 ", age=" + age + 38 ", a='" + a + '\'' + 39 ", b='" + b + '\'' + 40 ", c='" + c + '\'' + 41 ", d='" + d + '\'' + 42 '}'; 43 } 44 public void eat(String food){ 45 System.out.println("吃"+food); 46 } 47 48 }
1 //测试一,获取成员变量 2 3 import java.lang.reflect.Field; 4 5 public class test01 { 6 public static void main(String[] args) throws Exception { 7 //0.获取person的class对象 8 Class personClass=Person.class; 9 //获取成员变量,1、Field[] getFields():获取 10 Field[] fields=personClass.getFields(); 11 for(Field field:fields){ 12 System.out.println(field); 13 } 14 System.out.println("----------"); 15 //2、Field getField(String name):获取指定public成员变量 16 Field a=personClass.getField("a"); 17 Person p=new Person(); 18 Object value=a.get(p); 19 System.out.println(value); 20 a.set(p,"张三"); 21 System.out.println(p); 22 System.out.println("------------"); 23 //Field[] getDeclareFields():获取所有成员变量 24 Field[] declaredFields=personClass.getDeclaredFields(); 25 for(Field declaredField:declaredFields){ 26 System.out.println(declaredField); 27 } 28 //Field getDeclaredField(String name) 29 Field d= personClass.getDeclaredField("d"); 30 d.setAccessible(true);//暴力反射,忽略权限检查 31 Object value1=d.get(p); 32 System.out.println(value1); 33 } 34 35 }
1 //获取构造方法 2 import java.lang.reflect.Constructor; 3 4 public class test02 { 5 public static void main(String[] args) throws Exception { 6 Class personClass=Person.class; 7 //Constructor<T> getConstructor(类<?>...parameterTypes) 8 Constructor c=personClass.getConstructor(String.class,int.class); 9 System.out.println(c); 10 Object person=c.newInstance("张三",21); 11 System.out.println(person); 12 System.out.println("-------"); 13 Constructor c1= personClass.getConstructor(); 14 System.out.println(c1); 15 Object person1=c1.newInstance(); 16 System.out.println(person1); 17 } 18 }
1 //获取成员方法和类名 2 import java.lang.reflect.Method; 3 4 public class test03 { 5 public static void main(String[] args) throws Exception { 6 Class personClass=Person.class; 7 //获取指定名称方法 8 Method m=personClass.getMethod("eat"); 9 Person p=new Person(); 10 m.invoke(p); 11 System.out.println("------------"); 12 Method m1= personClass.getMethod("eat",String.class); 13 m1.invoke(p,"宵夜"); 14 System.out.println("------------"); 15 //获取所有public修饰方法 16 Method[] ms= personClass.getMethods(); 17 for(Method me:ms){ 18 System.out.println(me); 19 //String getName:获取方法名 20 String name= me.getName(); 21 System.out.println(name); 22 } 23 //获取类名 24 String className=personClass.getName(); 25 System.out.println(className); 26 } 27 }
其中有几个方法特殊
①setAccessible(true):忽略权限检查
②newInstance():简化获取构造方法操作
③invoke(Object obj):执行方法
④String getName:获取方法名