2009-07-11 11 views

risposta

3

A occhio e croce direi che questo è fatto riflettendo sul tipo di destinazione e l'impostazione dei campi direttamente utilizzando la riflessione

Io non sono un programmatore Java, ma credo che Java ha il supporto di riflessione simile a quello di .NET che uso

4

Prova

import java.lang.reflect.Field; 

class Test { 
    private final int value; 
    Test(int value) { this.value = value; } 
    public String toString() { return "" + value; } 
} 

public class Main { 
    public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { 
     Test test = new Test(12345); 
     System.out.println("test= "+test); 

     Field value = Test.class.getDeclaredField("value"); 
     value.setAccessible(true); 
     System.out.println("test.value= "+value.get(test)); 
     value.set(test, 99999); 
     System.out.println("test= "+test); 
     System.out.println("test.value= "+value.get(test)); 
    } 
} 

stampe

test= 12345 
test.value= 12345 
test= 99999 
test.value= 99999 
Problemi correlati