java基础——反射
反射概述:
java反射机制:是指在运行时去获取一个类的变量和方法信息。然后通过获取到的信息来创建对象,调用方法的一种机制。由于这种动态性,可以极大的增强程序的灵活性,程序不用在编译期就完成确定,在运行期仍然可以扩展。
获取Class类的对象
我们想要通过反射去使用一个类,首先我们要获取到该类的字节码文件对象,也就是类型为Class类型的对象
在这里我们提供了三种方法来获取。
1.使用类的class属性类获取该类对应的Class对象。举例worker.class将会返回worker类对应的class对象
2.调用对象的getClass()方法,返回该对象所属类对应的Class对象
该方法是Object类中的方法,所有的java对象都可以调用该方法
3.使用Class类中的静态方法forName(String className),该方法需要传入字符串参数,该字符串参数的值是某个类的全路径,也就是完整包名的路径
package com.aynu26;// 在这里我们提供了三种方法来获取。
//
// 1.使用类的class属性类获取该类对应的Class对象。举例worker.class将会返回worker类对应的class对象
//
// 2.调用对象的getClass()方法,返回该对象所属类对应的Class对象
//
// 该方法是Object类中的方法,所有的java对象都可以调用该方法
//
// 3.使用Class类中的静态方法forName(String className),该方法需要传入字符串参数,该字符串参数的值是某个类的全路径,也就是完整包名的路径public class ReflectDemo {public static void main(String[] args) throws ClassNotFoundException {//1.使用类的class属性类获取该类对应的Class对象。Class<Student> c1 = Student.class;System.out.println(c1);Class<Student> c2 = Student.class;System.out.println(c1 == c2);System.out.println("--------");//2.调用对象的getClass()方法,返回该对象所属类对应的Class对象Student s = new Student();Class<? extends Student> c3 = s.getClass();System.out.println(c1 == c3);System.out.println("--------");//3.使用Class类中的静态方法forName(String className)Class<?> c4 = Class.forName("com.aynu26.Student");System.out.println(c1 == c4);}
}
class com.aynu26.Student
true
--------
true
--------
true
反射获取构造方法并使用
Class类中用于获取构造方法的方法
1.Constructor<?>[] getConstructors(): 返回所有公共构造方法对象的数组
2.Constructor<?>[] getDeclaredConstructors(): 返回所有构造方法对象的数组
3.Constructor<T> getConstructor(Class<?>...parameterTypes):返回单个公共构造方法对象
4.Constructor<T> getDeclaredConstructor(Class<?>...parameterTypes): 返回单个构造方法对象
Constructor类中用于创建对象的方法
T newInstance(Object...initargs):根据指定的构造方法创建对象
反射获取构造方法并练习
练习1:通过反射实现如下操作
Student s =new Student("林青霞",30,"西安");
System.out.println(s);
基本数据类型也可以通过.class得到对应的class类型
package com.aynu26;//练习1:通过反射实现如下操作
//
// Student s =new Student("林青霞",30,"西安");
//
// System.out.println(s);import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class ReflectDemo1 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {//获取Class对象Class<?> c = Class.forName("com.aynu26.Student");//public Student(String name, int age, String address)//Constructor<T> getConstructor (Class<?>... parameterTypes)Constructor<?> con = c.getConstructor(String.class, int.class, String.class);//基本数据类型也可以通过.class得到对应的class类型//T newInstance (Object...initargs)Object obj = con.newInstance("林青霞", 30, "西安");System.out.println(obj);}
}
Student{name='林青霞', age=30, address='西安'}
练习2:通过反射实现如下操作
Student s =new Student("林青霞");
System.out.println(s);
public void setAccessible(boolean flag):值为true,取消访问检查
package com.aynu26;//练习2:通过反射实现如下操作
//
// Student s =new Student("林青霞");
//
// System.out.println(s);import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;public class ReflectDemo2 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {//获取Class对象Class<?> c = Class.forName("com.aynu26.Student");//private Student(String name)//Constructor<T> getDeclaredConstructor (Class<?>... parameterTypes)Constructor<?> con = c.getDeclaredConstructor(String.class);//暴力反射//public void setAccessible (boolean flag):值为true,取消访问检查con.setAccessible(true);Object obj = con.newInstance("林青霞");System.out.println(obj);}
}
Student{name='林青霞', age=0, address='null'}
反射获取成员变量并使用
Class类中用于给成员变量赋值的方法
Field[] getFields():返回所有公共成员变量对象的数组
Field[] getDeclaredField():返回所有成员变量对象的数组
Field getField(String name):返回单个公共成员变量对象
Field getDeclaredField(String name):返回单个成员变量对象
Field类中用于给成员变量赋值的方法
void set(Object obj,Object value):给obj对象的成员变量赋值为value
package com.aynu26;//反射获取成员变量并使用import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;public class RefiectDemo3 {public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {//获取Class对象Class<?> c = Class.forName("com.aynu26.Student");//Field[] getFields () 返回一个包含 Field对象的数组, Field对象反映由该 Class对象表示的类或接口的所有可访问的公共字段。//Field[] getDeclaredFields () 返回一个 Field对象的数组,反映了由该 Class对象表示的类或接口声明的所有字段。
// Field[] fields = c.getFields();Field[] fields = c.getDeclaredFields();for (Field field:fields){System.out.println(field);}System.out.println("--------");//Field getField (String name) 返回一个 Field对象,该对象反映由该 Class对象表示的类或接口的指定公共成员字段。//Field getDeclaredField (String name) 返回一个 Field对象,该对象反映由该 Class对象表示的类或接口的指定声明字段。Field addressField = c.getField("address");//获取无参构造方法创建对象Constructor<?> con = c.getConstructor();Object obj = con.newInstance();// obj.addressField="西安";//Field提供有关类或接口的单个字段的信息和动态访问//void set (Object obj, Object value) 将指定的对象参数中由此 Field对象表示的字段设置为指定的新值。addressField.set(obj,"西安"); //给obj的成员变量addressField赋值为西安System.out.println(obj);// Student s=new Student();
// s.address="西安";
// System.out.println(s);}
}
private java.lang.String com.aynu26.Student.name
int com.aynu26.Student.age
public java.lang.String com.aynu26.Student.address
--------
Student{name='null', age=0, address='西安'}
反射获取成员变量并使用练习
练习:通过反射实现如下操作
Student s =new Student();
s.name = "林青霞";
s.age = 30;
s.address = "西安";
System.out.println(s);
package com.aynu26;//练习:通过反射实现如下操作
//
// Student s =new Student();
//
// s.name = "林青霞";
//
// s.age = 30;
//
// s.address = "西安";
//
// System.out.println(s);import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;public class RelfectDemo4 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {//获取Class对象Class<?> c = Class.forName("com.aynu26.Student");//Student s =new Student();Constructor<?> con = c.getConstructor();Object obj = con.newInstance();System.out.println(obj);// Field nameField = c.getField("name");Field nameField = c.getDeclaredField("name");nameField.setAccessible(true);nameField.set(obj,"林青霞");System.out.println(obj);//s.age = 30;Field ageField = c.getDeclaredField("age");ageField.setAccessible(true);ageField.set(obj,30);System.out.println(obj);//s.address = "西安";Field addressField = c.getDeclaredField("address");addressField.setAccessible(true);addressField.set(obj,"西安");System.out.println(obj);}
}
Student{name='null', age=0, address='null'}
Student{name='林青霞', age=0, address='null'}
Student{name='林青霞', age=30, address='null'}
Student{name='林青霞', age=30, address='西安'}
反射获取成员方法并使用
Class类中用于获取成员方法的方法
Method[] getMethods(): 返回所有公共成员方法对象的数组,包括继承的
Method[] getDeclaredMethods(): 返回所有成员方法对象的数组,不包括继承的
Method getMethod(String name,Class<?>... parameterTypes):返回单个成员方法对象
Method类中用于调用成员方法的方法
Object invoke(Object obj,Object... args):调用obj对象的成员方法,参数是args,返回值是Object类型
package com.aynu26;//反射获取成员方法并使用import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class ReflectDemo5 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {//获取Class对象Class<?> c =Class.forName("com.aynu26.Student");//Method[] getMethods () 返回一个包含 方法对象的数组, 方法对象反映由该 Class对象表示的类或接口的所有公共方法,包括由类或接口声明的对象以及从超类和超级接口继承的类。//Method[] getDeclaredMethods () 返回一个包含 方法对象的数组, 方法对象反映由 Class对象表示的类或接口的所有声明方法,包括public,protected,default(package)访问和私有方法,但不包括继承方法。
// Method[] methods = c.getMethods();Method[] methods = c.getDeclaredMethods();for (Method method:methods){System.out.println(method);}System.out.println("--------");//Method getMethod (String name, Class<?>... parameterTypes) 返回一个 方法对象,该对象反映由该 Class对象表示的类或接口的指定公共成员方法。//方法 getDeclaredMethod (String name, Class<?>... parameterTypes) 返回一个 方法对象,它反映此表示的类或接口的指定声明的方法 Class对象。//public void method1()Method m = c.getMethod("method1");//获取无参构造方法创建对象Constructor<?> con = c.getConstructor();Object obj = con.newInstance();// obj.m();//在类或接口上提供有关单一方法的信息和访问权限//Object invoke (Object obj, Object... args) 在具有指定参数的指定对象上调用此 方法对象表示的基础方法//Object:返回值类型//obj:调用方法的对象//args:方法需要的参数m.invoke(obj);// Student s=new Student();
// s.method1();}
}
public java.lang.String com.aynu26.Student.toString()
private void com.aynu26.Student.function()
public void com.aynu26.Student.method2(java.lang.String)
public void com.aynu26.Student.method1()
public java.lang.String com.aynu26.Student.method3(java.lang.String,int)
--------
method
练习:通过反射实现如下操作
Student s =new Student();
s.method1();
s.method2("林青霞");
String ss =s.method3("林青霞",30);
System.out.println(ss);
s.function();
package com.aynu26;//练习:通过反射实现如下操作
// Student s =new Student();
// s.method1();
// s.method2("林青霞");
// String ss =s.method3("林青霞",30);
// System.out.println(ss);
// s.function();import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class ReflectDemo6 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {//获取Class对象Class<?> c = Class.forName("com.aynu26.Student");//Student s =new Student();Constructor<?> con = c.getConstructor();Object obj = con.newInstance();//s.method1();Method m1 = c.getMethod("method1");m1.invoke(obj);//s.method2("林青霞");Method m2 = c.getMethod("method2", String.class);m2.invoke(obj,"林青霞");// String ss =s.method3("林青霞",30);// System.out.println(ss);Method m3 = c.getMethod("method3", String.class, int.class);Object o = m3.invoke(obj, "林青霞", 30);System.out.println(o);// s.function();
// Method m4 = c.getMethod("function"); //NoSuchMethodException: com.aynu26.Student.function()Method m4=c.getDeclaredMethod("function");m4.setAccessible(true);m4.invoke(obj);}
}
method
method:林青霞
林青霞,30
function