B
This commit is contained in:
parent
e5e8880e41
commit
518da4a923
1019 changed files with 15877 additions and 0 deletions
29
Task/Break-OO-privacy/Java/break-oo-privacy.java
Normal file
29
Task/Break-OO-privacy/Java/break-oo-privacy.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import java.lang.reflect.*;
|
||||
|
||||
class Example {
|
||||
private String _name;
|
||||
public Example(String name) { _name = name; }
|
||||
public String toString() { return "Hello, I am " + _name; }
|
||||
}
|
||||
|
||||
public class BreakPrivacy {
|
||||
public static final void main(String[] args) throws Exception {
|
||||
Example foo = new Example("Eric");
|
||||
|
||||
for (Field f : Example.class.getDeclaredFields()) {
|
||||
if (f.getName().equals("_name")) {
|
||||
// make it accessible
|
||||
f.setAccessible(true);
|
||||
|
||||
// get private field
|
||||
System.out.println(f.get(foo));
|
||||
|
||||
// set private field
|
||||
f.set(foo, "Edith");
|
||||
System.out.println(foo);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue