gpt4 book ai didi

java - 获取非实例化类的字段值 - 反射

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:45:21 33 4
gpt4 key购买 nike

如何迭代并获取不可实例化类的所有字段的值:

import java.lang.reflect.Field;

public class NonInstantiableClass {
private Integer a1 = 1;
private String a2 = "a";

private NonInstantiableClass () {
throw new AssertionError();
}

public static void printVariables () throws IllegalAccessException {

for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
field.setAccessible(true);
System.out.println(field.getName()
+ " - " + field.getType()
+ " - " + field.get(NonInstantiableClass.class));
}
}


public static void main(String args[]) throws IllegalAccessException {
NonInstantiableClass.printVariables();
}
}

鉴于此代码出现以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Integer field NonInstanciableClass.a1 to java.lang.Class
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.base/java.lang.reflect.Field.get(Field.java:418)
at NonInstanciableClass.printVariables(NonInstanciableClass.java:17)
at NonInstanciableClass.main(NonInstanciableClass.java:23)

如果该类是可实例化的,则相同的代码可用于实例:

import java.lang.reflect.Field;

public class NonInstantiableClass {
private Integer a1 = 1;
private String a2 = "a";

//private NonInstantiableClass () {
// throw new AssertionError();
//}

public static void printVariables () throws IllegalAccessException {

for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
field.setAccessible(true);
System.out.println(field.getName()
+ " - " + field.getType()
+ " - " + field.get(new NonInstantiableClass()));
}
}


public static void main(String args[]) throws IllegalAccessException {
NonInstantiableClass.printVariables();
}
}

是否有更好的方法来解决这个问题?

谢谢。

编辑:

正如 Dylan 所指出的,如果类不可实例化,则实例字段无法初始化。当然字段是静态的:

private static Integer a1 = 1;
private static String a2 = "a";

最佳答案

试试这个:

import java.lang.reflect.Field;

public class NonInstantiableClass {
private static Integer a1 = 1;
private static String a2 = "a";

private NonInstantiableClass () {
throw new AssertionError();
}

public static void printVariables () throws IllegalAccessException {

for (Field field : NonInstantiableClass.class.getDeclaredFields()) {
field.setAccessible(true);
System.out.println(field.getName()
+ " - " + field.getType()
+ " - " + field.get(NonInstantiableClass.class));
}
}

public static void main(String args[]) throws IllegalAccessException {
NonInstantiableClass.printVariables();
}
}

虽然这只会告诉您静态字段。如果字段不是静态的,getVariableValue 返回 null。

关于java - 获取非实例化类的字段值 - 反射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54871271/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com