java反射

什么是反射?

Java反射就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;并且能改变它的属性。

反射作用?

我们知道反射机制允许程序在运行时取得任何一个已知名称的class的内部信息,包括包括其modifiers(修饰符),fields(属性),methods(方法)等,并可于运行时改变fields内容或调用methods。那么我们便可以更灵活的编写代码,代码可以在运行时装配,无需在组件之间进行源代码链接,降低代码的耦合度;还有动态代理的实现等等;

实现示例

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 反射的应用场合:在编译时根本无法知道该对象或类可能属于哪些类,
* 程序只依靠运行时信息来发现该对象和类的真实信息.
* @author wangming
*
*/
public class Person {
//私有属性
private String name = "test";

//公有属性
public int age = 20;

//公有属性
public int sax = 1;

//构造方法
public Person() {}

//私有方法
private void say() {
System.out.println("this is private method!");
}

//共有方法
public void work() {
System.out.println("this is public method!");
}

public static void main(String[] args) {
try {
//通过对象调用 getClass() 方法来获取,通常应用在:比如你传过来一个 Object
//类型的对象,而我不知道你具体是什么类,用这种方法
/*
* Person person = new Person(); Class c1 = person.getClass();
*/

//直接通过 类名.class 的方式得到,该方法最为安全可靠,程序性能更高
//这说明任何一个类都有一个隐含的静态成员变量 class
Class c2 = Person.class;
//通过 Class 对象的 forName() 静态方法来获取,用的最多,
//但可能抛出 ClassNotFoundException 异常
/*
* try { Class c3 = Class.forName("com.reflect.Person"); } catch
* (ClassNotFoundException e) { e.printStackTrace(); }
*/
//获得类完整的名字
// String className = c2.getName();
// System.out.println("className:"+className);

//获得类的public类型的属性
// Field[] fields = c2.getFields();
// for(Field field:fields) {
// System.out.println(field.getName());
// }

//获得类的所有属性。包括私有的
// Field[] AllFields = c2.getDeclaredFields();
// for(Field field:AllFields) {
// System.out.println(field.getName());
// }

//获得类的public类型的方法。这里包括 Object 类的一些方法
// Method[] methods = c2.getMethods();
// for(Method method:methods) {
// System.out.println(method.getName());
// }

//获得类的所有方法
// Method[] allMethods = c2.getDeclaredMethods();
// for(Method method:allMethods) {
// System.out.println(method.getName());
// }

//获得指定的属性
// Field f1 = c2.getField("age");
// System.out.println(f1);

//获得指定的私有属性
Field f2 = c2.getDeclaredField("name");
System.out.println(f2);

//创建这个类的一个对象
Object p2 = c2.newInstance();
//将 p2 对象的 f2 属性赋值为 Bob,f2 属性即为 私有属性 name
f2.set(p2, "test");
//使用反射机制可以打破封装性,导致了java对象的属性不安全
System.out.println(f2.get(p2));

//获取构造方法
Constructor[] constructors = c2.getConstructors();
for(Constructor constructor:constructors) {
System.out.println(constructor.getName());
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

原始链接

https://www.cnblogs.com/ysocean/p/6516248.html

本文标题:java反射

文章作者:wangming

发布时间:2019年01月21日 - 17:13:29

最后更新:2019年01月22日 - 23:36:25

原始链接:https://syxiaowanzi.github.io/2019/01/21/java反射/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%