1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
import java.lang.reflect.*;
class Fouineuse {
public static void main(String[] args) {
MonEntierSecret monEntierSecret=new MonEntierSecret();
int trouvé=jeTeVois(monEntierSecret);
System.out.println("Mon secret est : "+trouvé);
trouvé=teVoisJe(monEntierSecret);
System.out.println("Et rebelote : "+trouvé);
}
static int jeTeVois(Object monEntierSecret) {
Class classe=monEntierSecret.getClass();
Field monChamp1=null;
try {
monChamp1=classe.getDeclaredField("x");
} catch(NoSuchFieldException e) {
System.out.println(e.getMessage());
}
monChamp1.setAccessible(true);
int vu=0;
try {
vu=monChamp1.getInt(monEntierSecret);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch(IllegalAccessException e) {
System.out.println(e.getMessage());
}
return vu;
}
static int teVoisJe(Object monEntierSecret) {
Class classe=monEntierSecret.getClass();
Field monChamp2=null;
try {
monChamp2=classe.getDeclaredField("x");
} catch(NoSuchFieldException e) {
System.out.println(e.getMessage());
}
int vu=0;
try {
vu=monChamp2.getInt(monEntierSecret);
} catch(IllegalArgumentException e) {
System.out.println(e.getMessage());
} catch(IllegalAccessException e) {
System.out.println(e.getMessage());
}
return vu;
}
}
class MonEntierSecret {
private int x=4;
} |
Partager