反射获取构造方法对象并且创建对象
通过反射获取构造方法并且创建对象
* 1.通过反射获取字节码文件对象
* 2.通过字节码文件对象获取构造方法对象 Constructor
* Constructor<?>[] getConstructors() // 获取公有修饰的构造方法对象
* Constructor<?>[] getDeclaredConstructors() // 获取所有的构造方法对象
* Constructor<T> getConstructor(Class<?>... parameterTypes)
* Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
*/
public class ReflectDemo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
Class<?> c = Class.forName("com.sxt.reflectdemo2.Student");
Constructor<?>[] constructors = c.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("----------------------------------");
Constructor<?>[] declaredConstructors = c.getDeclaredConstructors();
for (Constructor<?> constructor : declaredConstructors) {
System.out.println(constructor);
}
System.out.println("----------------------------------");
// 获取所有的构造方法在开发中有意义? 没有什么实际意义,所以我们来获取单个构造方法
// 获取无参构造方法
// c.getConstructor(Class<?>... parameterTypes Class class)
Constructor<?> constructor = c.getConstructor();
System.out.println(constructor);
Constructor<?> constructor2 = c.getConstructor(String.class,int.class);
System.out.println(constructor2);
// 获取私有构造方法
// java.lang.NoSuchMethodException
// Constructor<?> constructor3 = c.getConstructor(String.class);
// System.out.println(constructor3);
Constructor<?> constructor3 = c.getDeclaredConstructor(String.class);
System.out.println(constructor3);
}
}
/*
* 通过反射获取构造方法对象并且创建对象
*
* 反射之前的做法:
* Student s = new Student();
*
* 反射之后:
* 1.获取字节码文件对象
* 2.通过字节码文件对象获取构造方法对象
* 3.通过构造方法对象创建对象
* T newInstance(Object... initargs)
*/
public class ReflectDemo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
FileNotFoundException, IOException {
Properties prop = new Properties();
prop.load(new FileReader("prop.txt"));
String value = prop.getProperty("ClassName");
Class<?> c = Class.forName(value);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
System.out.println(obj);
Student student = (Student) obj;
System.out.println(student.getName());
// 我想要调用带全参数的怎么办
Constructor<?> constructor = c.getConstructor(String.class, int.class);
Object obj2 = constructor.newInstance("隔壁老王", 50);
System.out.println(obj2);
// 我想获取私有构造方法并且造对象
// java.lang.NoSuchMethodException:
// Constructor<?> constructor2 = c.getConstructor(String.class);
Constructor<?> constructor2 = c.getDeclaredConstructor(String.class);
// java.lang.IllegalAccessException
// 暴力访问
constructor2.setAccessible(true);
Object obj3 = constructor2.newInstance("隔壁老李");
System.out.println(obj3);
}
}
反射获取成员变量对象并且赋值
通过反射获取成员变量对象
* Field
*
* Field[] getFields() 获取公有修饰的成员变量对象
* Field[] getDeclaredFields() 获取所有成员变量对象
* Field getField(String name) 获取指定的公共成员变量对象
* Field getDeclaredField(String name) 获取指定任意成员变量对象
*
* Object get(Object obj) 返回指定对象上此 Field 表示的字段的值。
*/
public class ReflectDemo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException {
Class<?> c = Class.forName("com.sxt.reflectdemo3.Student");
Field[] fields = c.getFields();
for (Field field : fields) {
System.out.println(field);
}
System.out.println("---------------");
Field[] declaredFields = c.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}
System.out.println("---------------");
// declaredFields
Field field = c.getField("address");
System.out.println(field);
System.out.println("---------------");
Field field2 = c.getDeclaredField("name");
System.out.println(field2);
}
}
public class ReflectDemo02 {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.sxt.reflectdemo3.Student");
// 给sex属性赋值,值为女
Field sexFiled = c.getDeclaredField("sex");
System.out.println(sexFiled);
// 反射之前 Student s = new Student(); s.sex = "女";
// 反射之后
Constructor<?> con = c.getDeclaredConstructor();
Object obj = con.newInstance();
// Object get(Object obj) 返回指定对象上此 Field 表示的字段的值。
sexFiled.set(obj, "女");
System.out.println(obj);
Field nameField = c.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(obj, "隔壁老张");
System.out.println(obj);
System.out.println(nameField.get(obj));
}
}
反射获取成员方法对象并且调用
通过反射获取成员方法对象并且调用
* c.getMethods() 获取所有公有方法 包括父类继承过来的成员
* c.getDeclaredMethods() 获取本类中所有方法,包括私有
*
* Method getMethod(String name, Class<?>... parameterTypes)
* Method getDeclaredMethod(String name, Class<?>... parameterTypes)
* 第一个参数是方法名称, 第二个参数是参数类型列表
*
* Object invoke(Object obj, Object... args)
*
* 使用反射书写一个能够给任意对象设置任意的值的方法
*/
public class ReflectDemo01 {
public static void main(String[] args) throws Exception {
Class<?> c = Class.forName("com.sxt.reflectdemo4.Student");
Constructor<?> con = c.getDeclaredConstructor();
Object obj = con.newInstance();
Method[] methods = c.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("--------------------------");
Method[] declaredMethods = c.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method);
}
System.out.println("--------------------------");
// 获取无参方法show并且调用
Method method = c.getDeclaredMethod("show");
System.out.println(method);
method.invoke(obj);
System.out.println("--------------------------");
// public void method(String s)
Method method2 = c.getDeclaredMethod("method", String.class);
System.out.println(method2);
method2.invoke(obj, "Hello");
System.out.println("--------------------------");
// public String concat(int a, String s, double d)
Method method3 = c.getDeclaredMethod("concat", int.class, String.class, double.class);
System.out.println(method3);
Object result = method3.invoke(obj, 20, "张三", 2.5);
System.out.println(result);
System.out.println("--------------------------");
//private void show2()
Method method4 = c.getDeclaredMethod("show2");
method4.setAccessible(true);
System.out.println(method4);
method4.invoke(obj);
// int getModifiers()
int modifiers = method4.getModifiers();
System.out.println(modifiers);
}
}
public class Student {
private String name;
int age;
public String address;
protected String sex;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
private Student(String name) {
this.name = name;
}
protected Student(String name,String address) {
this.address = address;
this.name = name;
}
Student(int age){
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", address=" + address + ", sex=" + sex + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void show() {
System.out.println("我是无参show方法");
}
public void method(String s) {
System.out.println("我是带Sting参数的method方法:" + s);
}
public String concat(int a, String s, double d) {
return a + s + d;
}
private void show2() {
System.out.println("我是私有方法");
}
}