av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語(yǔ)言

Java面試題如何通過反射獲取和設(shè)置對(duì)象私有字段的值

時(shí)間:2025-04-08 20:46:43 java語(yǔ)言 我要投稿
  • 相關(guān)推薦

Java面試題如何通過反射獲取和設(shè)置對(duì)象私有字段的值

  引導(dǎo)語(yǔ):private關(guān)鍵字定義的都是私有字段,以下是百分網(wǎng)小編分享給大家的Java面試題如何通過反射獲取和設(shè)置對(duì)象私有字段的值?歡迎閱讀學(xué)習(xí)!

Java面試題如何通過反射獲取和設(shè)置對(duì)象私有字段的值

  如何通過反射獲取和設(shè)置對(duì)象私有字段的值?

  答:可以通過類對(duì)象的getDeclaredField()方法字段(Field)對(duì)象,然后再通過字段對(duì)象的setAccessible(true)將其設(shè)置為可以訪問,接下來就可以通過get/set方法來獲取/設(shè)置字段的值了。下面的代碼實(shí)現(xiàn)了一個(gè)反射的工具類,其中的兩個(gè)靜態(tài)方法分別用于獲取和設(shè)置私有字段的值,字段可以是基本類型也可以是對(duì)象類型且支持多級(jí)對(duì)象操作,例如ReflectionUtil.get(dog, “owner.car.engine.id”);可以獲得dog對(duì)象的主人的汽車的引擎的ID號(hào)。

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
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 反射工具類
 * @author 駱昊
 *
 */
public class ReflectionUtil {
 
    private ReflectionUtil() {
        throw new AssertionError();
    }
 
    /**
     * 通過反射取對(duì)象指定字段(屬性)的值
     * @param target 目標(biāo)對(duì)象
     * @param fieldName 字段的名字
     * @throws 如果取不到對(duì)象指定字段的值則拋出異常
     * @return 字段的值
     */
    public static Object getValue(Object target, String fieldName) {
        Class<?> clazz = target.getClass();
        String[] fs = fieldName.split("\\.");
 
        try {
            for(int i = 0; i < fs.length - 1; i++) {
                Field f = clazz.getDeclaredField(fs[i]);
                f.setAccessible(true);
                target = f.get(target);
                clazz = target.getClass();
            }
 
            Field f = clazz.getDeclaredField(fs[fs.length - 1]);
            f.setAccessible(true);
            return f.get(target);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 通過反射給對(duì)象的指定字段賦值
     * @param target 目標(biāo)對(duì)象
     * @param fieldName 字段的名稱
     * @param value 值
     */
    public static void setValue(Object target, String fieldName, Object value) {
        Class<?> clazz = target.getClass();
        String[] fs = fieldName.split("\\.");
        try {
            for(int i = 0; i < fs.length - 1; i++) {
                Field f = clazz.getDeclaredField(fs[i]);
                f.setAccessible(true);
                Object val = f.get(target);
                if(val == null) {
                    Constructor<?> c = f.getType().getDeclaredConstructor();
                    c.setAccessible(true);
                    val = c.newInstance();
                    f.set(target, val);
                }
                target = val;
                clazz = target.getClass();
            }
 
            Field f = clazz.getDeclaredField(fs[fs.length - 1]);
            f.setAccessible(true);
            f.set(target, value);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
}

【Java面試題如何通過反射獲取和設(shè)置對(duì)象私有字段的值】相關(guān)文章:

java獲取Class對(duì)象的方法08-12

PHP如何設(shè)置和取得Cookie值08-07

JAVA如何獲取HTTP請(qǐng)求頭10-22

Java對(duì)象和類08-27

java中什么是字段07-23

如何檢測(cè)和設(shè)置路由器MTU值10-24

如何設(shè)置java的運(yùn)行環(huán)境10-15

s("download_bottom");