被反射的Student类
package reflect;
public class Student {
public String name;
private String age;
public int num;
public Student(){
}
public Student(String age) {
this.age = age;
}
private Student(String name, String age){
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
}
反射获取Class的三种方式
- Class.forName()
- 类名.class
- 类对象.getClass()
代码
//1
Class aClass = Class.forName("reflect.Student");
//2
Class bClass = Student.class;
//3
Student student = new Student();
Class cClass = student.getClass();
运行如下
反射获取构造方法
获取所有
- getConstructors() 只能获取public类型构造方法
- getDeclaredConstructors() 获取所有声明的构造方法
代码
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
//getConstructors()
Constructor[] constructor = aClass.getConstructors();
for(Constructor con : constructor){
System.out.println(con);
}
System.out.println();
//getDeclaredConstructors()
Constructor[] constructor2 = aClass.getDeclaredConstructors();
for(Constructor con : constructor2){
System.out.println(con);
}
}
运行如下
获取单个
declared差异和上面一样,至于获取哪个构造方法,由方法传入的参数类型决定
- getConstructor()
- getDeclaredConstructor()
代码
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
/*
public Student(){
}
public Student(String age) {
this.age = age;
}
private Student(String name, String age){
this.age = age;
this.name = name;
}
*/
Constructor constructor1 = aClass.getDeclaredConstructor();
Constructor constructor2 = aClass.getDeclaredConstructor(String.class);
Constructor constructor3 = aClass.getDeclaredConstructor(String.class,String.class);
System.out.println(constructor1);
System.out.println(constructor2);
System.out.println(constructor3);
运行如下
反射获取成员变量
其实和上面获取构造方法一样,只不过变成了field
获取所有
- getFields()
- getDeclaredFields()
代码
package reflect;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
Field[] fields = aClass.getFields();
for(Field field : fields){
System.out.println(field);
}
System.out.println();
Field[] fields2 = aClass.getDeclaredFields();
for(Field field : fields2){
System.out.println(field);
}
}
}
运行如下
获取单个
- getField()
- getDeclaredField()
括号中传入String类型的变量名
代码
package reflect;
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
Field field = aClass.getField("name");
System.out.println(field);
Field field2 = aClass.getDeclaredField("age");
System.out.println(field2);
}
}
运行如下
获取成员方法
当然是和前面基本一样了,不多解释了
获取所有
- getMethods()
- getDeclaredMethods()
代码
package reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
Method[] methods = aClass.getMethods();
for(Method method: methods){
System.out.println(method);
}
System.out.println();
Method[] methods2 = aClass.getDeclaredMethods();
for(Method method: methods2){
System.out.println(method);
}
}
}
运行如下
仔细观察,你会发现如果不加declare的方法,会连带父类的一些方法一起打印,这一点需要注意一下
最后是获取单个方法
反射方法最大的问题是什么?重载
怎么办呢,那就在反射前不仅声明要获取的方法名,同时也声明参数类型
比如String,就写String.class;int就写int
- getMethod()
- getDeclaredMethod()
代码
package reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
//获取反射类Student
Class aClass = Class.forName("reflect.Student");
Method method = aClass.getMethod("setName", String.class);
System.out.println(method);
Method method2 = aClass.getDeclaredMethod("getName");
System.out.println(method2);
}
}
运行如下
最后,记得要抛出异常奥,要不然拒绝服务就来了