Java 反射
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
反射机制的相关类
| 类名 |
用途 |
| Class类 |
代表类的实体,在运行的Java应用程序中表示类和接口 |
| Field类 |
代表类的成员变量(成员变量也称为类的属性) |
| Method类 |
代表类的方法 |
| Constructor类 |
代表类的构造方法 |
Class类
Class代表类的实体,在运行的Java应用程序中表示类和接口。在这个类中提供了很多有用的方法,这里对他们简单的分类介绍。
获取Class的三种方式
String name = "ZhangSan"; Class c1 = name.getClass(); System.out.println(c1.getName());
|
String name = "java.lang.String"; Class c1 = null; try { c1 = Class.forName(name); System.out.println(c1.getName()); } catch (ClassNotFoundException e) { }
|
Type属性
基本类型都有type属性,可以得到这个基本类型的类型,比如:
Class c1 = Boolean.TYPE; Class c2 = Byte.TYPE; Class c3 = Float.TYPE; Class c4 = Double.TYPE;
|
获取类的构造函数
public class Test { private int age; private String name; private int testint; public Test(int age) { this.age = age; } public Test(int age, String name) { this.age = age; this.name = name; System.out.println("hello " + name + "i am " + age); } private Test(String name) { this.name = name; System.out.println("My Name is " + name); } public Test() { } private void welcome(String tips){ System.out.println(tips); }
|
获取所有构造方法
Test test = new Test(); Class c4 = test.getClass(); Constructor[] constructors = c4.getDeclaredConstructors();
|
getDeclaredConstructors可以返回类的所有构造方法
getModifiers可以得到构造方法的类型
getParameterTypes可以得到构造方法的所有参数
for (int i = 0; i < constructors.length; i++) { System.out.print(Modifier.toString(constructors[i].getModifiers()) + "参数:"); Class[] parametertypes = constructors[i].getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getName() + " "); } System.out.println(""); }
|
结果:
public参数: public参数: java.lang.String public参数: int java.lang.String public参数: int
|
获取类中特定的构造方法
getDeclaredConstructor传参获取特定参数类型的构造方法
try{ Constructor constructor = c4.getDeclaredConstructor(); System.out.print(Modifier.toString(constructors.getModifiers())); }catch(NoSuchMethodException e){ e.printStackTrace(); }
|
结果:
Class[] p = {int.class,String.class}; try { constructors = c4.getDeclaredConstructor(p); System.out.print(Modifier.toString(constructors.getModifiers()) + "参数:"); Class[] parametertypes = constructors.getParameterTypes(); for (int j = 0; j < parametertypes.length; j++) { System.out.print(parametertypes[j].getName() + " "); } } catch (NoSuchMethodException e) { e.printStackTrace(); }
|
结果:
public参数: int java.lang.String
|
调用构造方法
Class[] p = {int.class,String.class}; constructors = c4.getDeclaredConstructor(p); constructors.newInstance(23,"LeeSkyWave");
|
结果:
调用私有构造方法
Class[] p = {String.class}; constructors = c4.getDeclaredConstructor(p); constructors.setAccessible(true); constructors.newInstance("LeeSkyWave");
|
结果:
获取类的成员方法
调用类的私有方法
Class[] p4 = {String.class}; Method method = c4.getDeclaredMethod("welcome",p4); method.setAccessible(true);
Object arg1s[] = {"welcone to my room"};
method.invoke(test,arg1s);
|
结果:
获取类的成员变量
获取私有成员变量并修改值
Field field = c4.getDeclaredField("name"); field.setAccessible(true);
field.set(o,"LeeSkyWave");
|
参考
https://blog.csdn.net/sinat_38259539/article/details/71799078
https://blog.csdn.net/huangliniqng/article/details/88554510