反射
最近在看设计模式相关的内容,部分模式使用到了java反射机制,因此重新复习了关于java反射的内容。
Class类
Class 是一个类,封装了对象实例所在类的相关信息,有系统创建,且每个类只有一个Class类
Class的创建以及对象的创建
用于测试的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class Person {
private String name; private Integer age;
public Person() { super(); }
public Person(String name, Integer age) { super(); this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
private void say() { System.out.println("hello"); }
}
|
Class类的创建以及使用class创建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
@Test public void CreateClass() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class<Person> class1 = Person.class; System.out.println(class1); Class<Person> class2 = (Class<Person>) Class.forName("com.huan.reflect.Person"); System.out.println(class2); Person person = new Person(); Class<Person> class3 = (Class<Person>) person.getClass(); System.out.println(class3);
Person p1 = class1.newInstance(); Person p2 = class2.newInstance(); Person p3 = class3.newInstance(); System.out.println(p1+"\n"+p2+"\n"+p3); }
|
Class获取方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
@Test public void getMethod() throws Exception { Class<Person> personClass = Person.class; Method[] methods = personClass.getMethods(); for (int s = 0; s < methods.length; s++) { System.out.print(" "+methods[s].getName()); } System.out.println(); Method[] selfMethods = personClass.getDeclaredMethods(); for (int i = 0; i < selfMethods.length; i++) { System.out.print(" "+selfMethods[i].getName()); } System.out.println(); Method say = personClass.getDeclaredMethod("say"); System.out.println(say); Method setName = personClass.getDeclaredMethod("setName", String.class); System.out.println(setName); Person person = personClass.newInstance(); setName.invoke(person, "昊天"); System.out.println(person.getName()); say.setAccessible(true); say.invoke(person); }
|
Class 获取字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
@Test public void getField() throws Exception { Class<Person> personClass = Person.class; Field[] fields = personClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { System.out.println(" "+fields[i]); } Field name = personClass.getDeclaredField("name"); System.out.println(name.getName()); Person person = new Person("昊天",10000); name.setAccessible(true); Object object = name.get(person); System.out.println(object); name.set(person, "桑桑"); System.out.println(person.getName()); }
|
Class 获取构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Test public void getConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class<Person> personClass = Person.class; Constructor<Person>[] declaredConstructors = (Constructor<Person>[]) personClass.getConstructors(); for (int i = 0; i < declaredConstructors.length; i++) { System.out.println(declaredConstructors[i]); } Constructor<Person> constructor = personClass.getConstructor(String.class,Integer.class); System.out.println(constructor); Person person = constructor.newInstance("昊天",12000); System.out.println(person.getName()+"|"+person.getAge()); }
|