Keresés

Új hozzászólás Aktív témák

  • Karma

    félisten

    válasz emvy #5769 üzenetére

    Sőt, ha már reflexióval birizgáljuk őket, hozzá is lehet férni az értékéhez (persze ha nincs security manager az útban).

    import java.lang.reflect.Field;

    public class HelloWorld{
    public static class A {
    private int value;
    public A(int value) {
    this.value = value;
    }
    }

    public static class B extends A {
    public B(int value) {
    super(value);
    }
    }

    public static void main(String []args){
    B b = new B(42);

    try {
    Field valueField = A.class.getDeclaredField("value");
    valueField.setAccessible(true);
    int internal = valueField.getInt(b);
    System.out.printf("Found value: %d%n", internal);
    } catch (Exception ex) {
    System.out.printf("Reflection failed: %s%n", ex.toString());
    }
    }
    }

    Ez az előbbi peremfeltétellel azt írja ki, hogy "Found value: 42".

Új hozzászólás Aktív témák