被反射的Student类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 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 2 3 4 5 6 7
| Class aClass = Class.forName("reflect.Student");
Class bClass = Student.class;
Student student = new Student(); Class cClass = student.getClass();
|
反射获取构造方法
获取所有
getConstructors()
只能获取public类型构造方法
getDeclaredConstructors()
获取所有声明的构造方法
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Class aClass = Class.forName("reflect.Student");
Constructor[] constructor = aClass.getConstructors(); for(Constructor con : constructor){ System.out.println(con); } System.out.println();
Constructor[] constructor2 = aClass.getDeclaredConstructors(); for(Constructor con : constructor2){ System.out.println(con); } }
|
获取单个
declared差异和上面一样,至于获取哪个构造方法,由方法传入的参数类型决定
- getConstructor()
- getDeclaredConstructor()
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Class aClass = Class.forName("reflect.Student");
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()
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package reflect;
import java.lang.reflect.Field;
public class Main { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException { 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类型的变量名
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package reflect;
import java.lang.reflect.Field;
public class Main { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { 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()
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package reflect;
import java.lang.reflect.Field; import java.lang.reflect.Method;
public class Main { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { 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()
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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 { 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); }
}
|
最后,记得抛出异常,否则可能导致拒绝服务
CSDN链接:Java反射类、构造方法、类变量、类方法_java类的反射 变量类型-CSDN博客