java反射修改属性值

tech2023-02-05  108

package com.mtons.mblog; /** * java反射修改属性值 */ import java.lang.reflect.Field; public class FinalTest { private final double a; { Class<FinalTest> aClass = FinalTest.class; Field field = null; try { field = aClass.getDeclaredField("a"); field.setAccessible(true); // field.set(this, 1000.0); Object o = field.get(this); System.err.println(o); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } public FinalTest() { this.a = 10.0; } public static void main(String[] args) throws Exception { FinalTest test = new FinalTest(); System.out.println(test.a); } }
最新回复(0)