反射_通过反射获取成员变量、构造方法、成员方法的使用
原创
©著作权归作者所有:来自51CTO博客作者wx62dfdc6aea345的原创作品,请联系作者获取转载授权,否则将追究法律责任
package cn.itcast_01;
public class Person {
private String name;
int age;
public String address;
public Person() {
}
private Person(String name) {
this.name = name;
}
Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void show() {
System.out.println("show");
}
public void method(String s) {
System.out.println("method:" + s);
}
public String getString(String name, int age) {
return name + "----" + age;
}
private void function() {
System.out.println("function");
}
@Override
public String toString() {
return "Person [" + "name=" + name + ", " + "age=" + age + ", "
+ "address=" + address + "]";
}
}
package cn.itcast_04;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/*
* 需求:通过反射对Person进行操作使用
*
* private String name;
* int age;
* public String address;
*
* public Person()
* private Person(String name)
* Person(String name, int age)
* public Person(String name, int age, String address)
*
* public void show()
* public void method(String s)
* public String getString(String name, int age)
* private void function()
*/
public class ReflectDemo2 {
public static void main(String[] args) throws Exception {
// 获取字节码文件对象
Class c = Class.forName("cn.itcast_01.Person");
// 获取无参构造方法对象
Constructor con = c.getConstructor();
// 通过无参构造方法对象创建对象
Object obj = con.newInstance();
System.out.println(obj);
// 通过反射给所有成员变量赋值
System.out.println("****通过反射给所有成员变量赋值****");
// private String name;
Field nameField = c.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(obj, "风清杨");
System.out.println(obj);
// int age;
Field ageField = c.getDeclaredField("age");
ageField.setAccessible(true);
ageField.set(obj, 27);
System.out.println(obj);
// public String address;
Field addressField = c.getField("address");
addressField.set(obj, "上海");
System.out.println(obj);
// 通过反射给所有构造方法赋值
System.out.println("****通过反射给所有构造方法赋值****");
// public Person()
// private Person(String name)
Constructor con2 = c.getDeclaredConstructor(String.class);
con2.setAccessible(true);
Object obj2 = con2.newInstance("王小明");
System.out.println(obj2);
// Person(String name, int age)
Constructor con3 = c.getDeclaredConstructor(String.class, int.class);
con3.setAccessible(true);
Object obj3 = con3.newInstance("林子聪", 35);
System.out.println(obj3);
// public Person(String name, int age, String address)
Constructor con4 = c.getConstructor(String.class, int.class,
String.class);
Object obj4 = con4.newInstance("江小斐", 56, "山东");
System.out.println(obj4);
// 通过反射调用所有成员方法
System.out.println("****通过反射调用所有成员方法****");
// public void show()
Method m1 = c.getMethod("show");
m1.invoke(obj);
// public void method(String s)
Method m2 = c.getMethod("method", String.class);
m2.invoke(obj, "hello");
// public String getString(String name, int age)
Method m3 = c.getMethod("getString", String.class, int.class);
Object objString = m3.invoke(obj, "world", 100);
System.out.println(objString);
// private void function()
Method m4 = c.getDeclaredMethod("function");
m4.setAccessible(true);
m4.invoke(obj);
}
}