1,什么是反射
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制
2,用反射获取class类
获取反射类有三种方法Class.forName("类路径") 类名.class对象.getClass
以下是通过反射获取class的三种方法的代码实现
public class fanshe {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
//通过类路径获取
Class<?> aClass = Class.forName("com.fanshe.demo01.Student");
//通过类名.calss获取
Class<Student> studentClass = Student.class;
//通过反射类.newInstance(),拿到反射类对象
Student student1 = studentClass.newInstance();
System.out.println(student1);
//通过new类对象来获取
Student student = new Student();
Class<? extends Student> aClass1 = student.getClass();
}
}
class Student{
private String name;
public Student() {
}
3. 通过反射类获取类对象
通过反射类.newInstance(),拿到反射类对象
//通过反射类.newInstance(),拿到反射类对象
Student student1 = studentClass.newInstance();
4.获取反射类中的属性和成员变量
获取反射类中的属性和成员变量有四种分别是:
getFields()//获取本类和父类的所有有公共属性的成员变量
getDeclaredFields()//获取本类所有成员变量
getField()//获取本类和父类的指定的公共变量
getDeclaredField()//获取本类指定变量
以下是代码实现
public class Text {
public static void main(String[] args) throws NoSuchFieldException {
//获取反射对象
Class<Student> studentClass = Student.class;
//获取本类和父类的所有公共变量
Field[] fields = studentClass.getFields();
//获取本类所有变量
Field[] declaredFields = studentClass.getDeclaredFields();
//获取本类和父类的指定的公共变量
Field field = studentClass.getField("name");
//获取本类指定变量
Field declaredField = studentClass.getDeclaredField("studentName");
}
}
class person{
public String name;
private int age;
}
class Student extends person{
public String studentName;
private int studentAge;
}
4.1Field中常见的方法
setAccessible(true): 设置允许访问私有属性
getAnnotation(注解.class):获取属性上的注解对象
可以通过成员变了.set(类对象,值)给成员变量赋值
以下代码是Field中常见的方法实现
public class Text {
public static void main(String[] args) throws NoSuchFieldException, InstantiationException, IllegalAccessException {
//获取反射对象
Class<Student> studentClass = Student.class;
Student student = studentClass.newInstance();
//获取本类和父类的所有公共属性
Field[] fields = studentClass.getFields();
//获取本类所有属性
Field[] declaredFields = studentClass.getDeclaredFields();
//获取本类和父类的指定的公共属性
Field field = studentClass.getField("name");
//获取本类指定属性
Field declaredField = studentClass.getDeclaredField("studentName");
//通过成员变了.set(类对象,值)给成员变量赋值
declaredField.set(student,"小明");
System.out.println(student);
//获取私有权限修饰的成员变量
Field declaredField1 = studentClass.getDeclaredField(" studentAge");
//开启访问私有权限
declaredField1.setAccessible(true);
declaredField1.set(student,16);
System.out.println(student);
//获取成员变量的注解
my annotation = declaredField1.getAnnotation(my.class);
System.out.println(annotation);
}
}
class person{
public String name;
private int age;
}
class Student extends person{
@my
public String studentName;
private int studentAge;
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface my{
String value() default "小红";
5,获取方法类
//通过反射类获取本类和父类公共所有方法对象 getDeclaredMethod() //通过反射类获取本类所有方法对像 getDeclaredMethods() //通过反射类获取本类和父类公共指定的方法对象 getMethods() //通过反射类获取本类指定方法对象 getMethod()
以下是代码实现部分
public class Text01 {
//获取反射类对象
public static void main(String[] args) throws NoSuchMethodException {
//获取反射类对象
Class<Student01> student01Class = Student01.class;
//通过反射类获取本类和父类公共所有方法对象
student01Class.getMethods();
//通过反射类获取本类所有方法对象
student01Class.getDeclaredMethods();
// 通过反射类获取本类和父类公共指定的方法对象
student01Class.getMethod("fun1",int.class);
//通过反射类获取本类指定方法对象
student01Class.getDeclaredMethod("fun");
}
}
class Student01{
public String fun(){
return "1";
}
public String fun1(int a){
System.out.println(a);
return "2";
5.1Method中常用的方法
invoke(): 执行该方法体。
getAnnotation()开启私有访问权限
以下代码是Method中常用的方法实现
public class Text01 {
//获取反射类对象
public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
//获取反射类对象
Class<Student01> student01Class = Student01.class;
Student01 student01 = student01Class.newInstance();
//通过反射类获取本类和父类公共所有方法对象
student01Class.getMethods();
//通过反射类获取本类所有方法对象
student01Class.getDeclaredMethods();
// 通过反射类获取本类和父类公共指定的方法对象
student01Class.getMethod("fun1",int.class);
//通过反射类获取本类指定方法对象
Method fun = student01Class.getDeclaredMethod("fun", String.class);
Object invoke = fun.invoke(student01,"23");//使用该方法
my01 annotation = fun.getAnnotation(my01.class);
System.out.println(annotation.value());
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface my01{
String value() default "11";
}
class Student01{
public String fun(String b){
System.out.println(b);
return "1";
}
public String fun1(int a){
System.out.println(a);
return "2";
}
private void fun2(){
System.out.println(3);
}
6,获取构造对象
得到本类中public的构造函数 getConstructors()
得到本类中所有的构造函数
getDeclaredConstructors()
得到本类中public的指定构造函数
getConstructor()
得到本类中指定构造函数
getDeclaredConstructor()
以下是代码实现
public class Text02 {
public static void main(String[] args) throws NoSuchMethodException {
Class<fun> funClass = fun.class;
funClass.getConstructors();
funClass.getDeclaredConstructors();
Constructor<fun> constructor = funClass.getConstructor(int.class);
Constructor<fun> declaredConstructor = funClass.getDeclaredConstructor(String.class);
}
}
class fun{
public fun(){
System.out.println("无参构造");
}
public fun(int a){
System.out.println(a);
}
private fun (String b){
System.out.println(b);
}
}
6.1Constructor中常见的方法
获取构造方法.newInstance().
以下是代码实现
public class Text02 {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<fun> funClass = fun.class;
funClass.getConstructors();
funClass.getDeclaredConstructors();
Constructor<fun> constructor = funClass.getConstructor(int.class);
Constructor<fun> declaredConstructor = funClass.getDeclaredConstructor(String.class);
declaredConstructor.setAccessible(true);
fun fun = declaredConstructor.newInstance("姬霓太美");//使用该构造方法病可以赋值
}
}
class fun{
public fun(){
System.out.println("无参构造");
}
public fun(int a){
System.out.println(a);
}
private fun (String b){
System.out.println(b);
}
}