反射是什么
反射允许对成员变量,成员方法和构造方法的信息进行编程访问
获取class对象的三种方式
代码
package a2;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name,int age){
this.name =name;
this.age =age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
age a2;
public class MyReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//第一种,全类名,包名+类名
Class clazz1 = Class.forName("a2.Student");
//第二种 更多是当做参数进行传递
Class clazz2 =Student.class;
//第三种 已经有这个类的对象才可以使用
Student s=new Student();
Class clazz3=s.getClass();
System.out.println(clazz1 == clazz2);
System.out.println(clazz2 == clazz3);
}
构造方法
代码
获取构造方法
package a3;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取class字节码文件对象
Class clazz = Class.forName("a3.Student");
//获取构造方法
// Constructor[] cons = clazz.getConstructors();
// for (Constructor con : cons) {
// System.out.println(con);
// }
// Constructor[] cons2= clazz.getDeclaredConstructors();
// for (Constructor constructor : cons2) {
// System.out.println(cons2);
// }
// Constructor con1=clazz.getDeclaredconstructor();
// System.out.println(con1);
// Constructor con2=clazz.getDeclaredconstructor(string.class);
// System.out.println(con2);
Constructor con3 = clazz.getDeclaredConstructor(String.class, int.class);
// int modifiers = con3.getModifiers();
// System.out.println(modifiers);
// Parameter[] parameters = con3.getParameters();
// for (Parameter parameter : parameters) {
// System.out.println(parameter);
//
// 创建对象
con3.setAccessible(true);
Student stu=(Student) con3.newInstance("张三",23);
System.out.println(stu);
}
}
package a3;
public class Student {
private String name;
private int age;
public Student(){
}
public Student(String name){
this.name =name;
}
protected Student(int age){
this.age=age;
}
private Student(String name,int age){
this.name =name;
this.age =age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
获取成员变量
代码
package a4;
public class Student {
private String name;
private int age;
public String gender;
public Student() {
}
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return gender
*/
public String getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(String gender) {
this.gender = gender;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + ", gender = " + gender + "}";
}
}
package a4;
import java.io.File;
import java.lang.reflect.Field;
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
//获取class字节码文件对象
Class<?> clazz = Class.forName("a4.Student");
//获取成员变量
// Field[] fileds= clazz.getFields();
// for (Field field : fileds) {
// System.out.println(field);
// }
//获取所有成员变量
// Field[] fileds= clazz.getDeclaredFields();
// for (Field field : fileds) {
// System.out.println(field);
// }
//获取单个的成员变量
// Field gender = clazz.getField("gender");
// System.out.println(gender);
Field name = clazz.getDeclaredField("name");
System.out.println(name);
//获取权限修饰符
int modifiers = name.getModifiers();
System.out.println(modifiers);
//获取成员变量的名字
String n =name.getName();
System.out.println(n);
//获取成员变量的数据类型
Class<?> type = name.getType();
System.out.println(type);
//获取成员变量记录的值
Student s =new Student();
name.setAccessible(true);
System value = (System) name.get(s);
System.out.println(value);
//修改对象里面记录的值
name.set(s,"list");
System.out.println(s);
}
}
获取成员方法
代码
package a5;
import javax.naming.Name;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//1.获取class字节码文件对象
Class clazz = Class.forName("a5.Student");
//获取里面所有的方法对象(包含父类所以公共方法)
// Method[] methods = clazz.getMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
//获取里面所有的方法对象(不能获取父类的,但是可以获取本类中的私有)
// Method[] methods = clazz.getDeclaredMethods();
// for (Method method : methods) {
// System.out.println(method);
// }
//获取指定单一方法
Method m = clazz.getDeclaredMethod("eat", String.class);
System.out.println(m);
//获取方法的修饰符
int modifiers = m.getModifiers();
System.out.println(modifiers);
//获取方法的名字
String name = m.getName();
System.out.println(name);
//获取方法的形参
Parameter[] parameters = m.getParameters();
for (Parameter parameter : parameters) {
System.out.println(parameter);
}
//获取方法抛出的异常
Class[] exceptionTypes = m.getExceptionTypes();
for (Class exceptionType : exceptionTypes) {
System.out.println(exceptionType);
}
//方法运行
Student s =new Student();
m.setAccessible(true);
Object result =(String)m.invoke(s,"汉堡王");
System.out.println(result);
}
}
kage a5;
import java.io.IOException;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
/**
* 获取
*
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
*
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
*
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public void sleep() {
System.out.println("睡觉");
}
private String eat(String something) throws IOException,NullPointerException,ClassCastException {
System.out.println("在吃" + something);
return "奥利给";
}
private void eat(String something,int a) {
System.out.println("在吃" + something);
}
public String toSring() {
return "Student{name =" + "name +" + "age =+ age +}";
}
}