Java反射~设置基元类型的内部对象值
我有一个int、short、byte或long类型的对象,我需要给它一个新值。这在Java中可能吗?如果是,怎么做Java反射~设置基元类型的内部对象值,java,reflection,integer,primitive,Java,Reflection,Integer,Primitive,我有一个int、short、byte或long类型的对象,我需要给它一个新值。这在Java中可能吗?如果是,怎么做 public static void set(Object obj, int value) throws Exception { Class<?> c = obj.getClass(); if (c.equals(Integer.class)) { // ??? } } publicstaticvoidset(objec
public static void set(Object obj, int value) throws Exception
{
Class<?> c = obj.getClass();
if (c.equals(Integer.class))
{
// ???
}
}
publicstaticvoidset(objectobj,int-value)抛出异常
{
c类=obj.getClass();
if(c.equals(Integer.class))
{
// ???
}
}
整数是不可变的。不能将值设置为整数
实例
类似地,原语类型的其他包装类也是不可变的。Integer是不可变的。不能将值设置为
整数
实例
类似地,原语类型的其他包装类也是不可变的。是的,只要您知道要处理的原语类型
Class clazz = Class.forName("TheClass");
Field f = clazz.getDeclaredField("ThePrimitiveField");
Object obj;
f.setBoolean(obj, true);
这将更改obj的“基本字段”字段。如果你不知道类型
Field f;
Object obj;
try {
f.setBoolean(obj, true);
} catch (IllegalArgumentException ex) {
try {
f.setByte(obj, 16);
} catch (IllegalArgumentException ex) {
try {
f.setChar(obj, 'a');
// etc
}
}
}
是的,只要你知道你要处理的是什么基本类型
Class clazz = Class.forName("TheClass");
Field f = clazz.getDeclaredField("ThePrimitiveField");
Object obj;
f.setBoolean(obj, true);
这将更改obj的“基本字段”字段。如果你不知道类型
Field f;
Object obj;
try {
f.setBoolean(obj, true);
} catch (IllegalArgumentException ex) {
try {
f.setByte(obj, 16);
} catch (IllegalArgumentException ex) {
try {
f.setChar(obj, 'a');
// etc
}
}
}
如果您知道类型,请执行以下操作:
public class Main
{
public static void main(String[] args)
throws NoSuchFieldException,
IllegalArgumentException,
IllegalAccessException
{
Foo fooA;
Foo fooB;
final Class<?> clazz;
final Field field;
fooA = new Foo();
fooB = new Foo();
clazz = fooA.getClass();
field = clazz.getDeclaredField("bar");
System.out.println(fooA.getBar());
System.out.println(fooB.getBar());
field.setAccessible(true); // have to do this since bar is private
field.set(fooA, 42);
System.out.println(fooA.getBar());
System.out.println(fooB.getBar());
}
}
class Foo
{
private int bar;
public int getBar()
{
return (bar);
}
}
如果您知道类型,请执行以下操作:
public class Main
{
public static void main(String[] args)
throws NoSuchFieldException,
IllegalArgumentException,
IllegalAccessException
{
Foo fooA;
Foo fooB;
final Class<?> clazz;
final Field field;
fooA = new Foo();
fooB = new Foo();
clazz = fooA.getClass();
field = clazz.getDeclaredField("bar");
System.out.println(fooA.getBar());
System.out.println(fooB.getBar());
field.setAccessible(true); // have to do this since bar is private
field.set(fooA, 42);
System.out.println(fooA.getBar());
System.out.println(fooB.getBar());
}
}
class Foo
{
private int bar;
public int getBar()
{
return (bar);
}
}
您想在obj实例中设置值,还是想设置对象的属性?@GaborSch obj是一个整数,所以我想设置它的值。您想要int还是integer?虽然不能修改包装器类,但可以将整数设置为新值。请检查我答案的底部,看一个这样做的例子。您想在obj实例中设置一个值,还是想设置一个对象的属性?@GaborSch obj是一个整数,所以我想设置它的值。您想要int还是integer?虽然不能修改包装器类,但可以将整数设置为新值。请检查我答案的底部,看一个这样做的例子。好吧,谢谢!这解决了我的麻烦。我得四处走走,穿过田野。不客气。是的,您必须通过字段设置它,或者引入包装器对象。好的,谢谢!这解决了我的麻烦。我得四处走走,穿过田野。不客气。是的,您必须通过字段设置它,或者引入包装器对象。是的,谢谢!这是我的B计划。我在这里问是因为我不想更改A计划的代码。^^是的,谢谢!这是我的B计划。我在这里问是因为我不想更改A计划的代码^^