package com.reflection;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Properties;
public class ReflectionTest {
@Test
public void reflectionStudy() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<Person> personClass = Person.class;
Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
Person p1 = constructor.newInstance("张三", 12);
System.out.println(p1.toString());
Field name = personClass.getDeclaredField("age");
name.set(p1,14);
System.out.println(p1.toString());
Method showNation = personClass.getDeclaredMethod("showNation", String.class);
Object showNationInvoke = showNation.invoke(p1, "中国");
Constructor<Person> declaredConstructor = personClass.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
Person p2 = declaredConstructor.newInstance("王五");
System.out.println(p2.toString());
Field name1 = personClass.getDeclaredField("name");
name1.setAccessible(true);
name1.set(p2,"赵六");
System.out.println(p2.toString());
Method showMe = personClass.getDeclaredMethod("showMe", String.class);
showMe.setAccessible(true);
Object act = showMe.invoke(p2, "跳跃");
System.out.println(act);
}
@Test
public void reflectionStudy1() throws ClassNotFoundException {
Class<Person> personClass = Person.class;
Person person = new Person();
Class<? extends Person> personClass1 = person.getClass();
Class<?> forName = Class.forName("com.reflection.Person");
ClassLoader classLoader = ReflectionTest.class.getClassLoader();
Class<?> loadClass = classLoader.loadClass("com.reflection.Person");
System.out.println(personClass==personClass1);
System.out.println(personClass==forName);
System.out.println(personClass==loadClass);
}
@Test
public void reflectionStudy2() throws IOException {
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("data2.properties");
FileInputStream fileInputStream1 = new FileInputStream("src\\data1.properties");
properties.load(fileInputStream1);
Object user = properties.get("user");
Object password = properties.get("password");
System.out.println(user+"\t"+password);
}
}
package com.reflection;
public class Person {
private String name;
public int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person() {
}
private Person(String name) {
this.name=name;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void showNation(String nation) {
System.out.println("我是" + nation);
}
private String showMe(String activity) {
System.out.println("我可以" + activity);
return activity;
}
}
|