Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Break-OO-privacy/Ecstasy/break-oo-privacy.ecstasy
Normal file
45
Task/Break-OO-privacy/Ecstasy/break-oo-privacy.ecstasy
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
module BreakOO {
|
||||
/**
|
||||
* This is a class with public, protected, and private properties.
|
||||
*/
|
||||
class Exposed {
|
||||
public String pub = "public";
|
||||
protected String pro = "protected";
|
||||
private String pri = "private";
|
||||
|
||||
@Override
|
||||
String toString() {
|
||||
return $"pub={pub.quoted()}, pro={pro.quoted()}, pri={pri.quoted()}";
|
||||
}
|
||||
}
|
||||
|
||||
void run() {
|
||||
@Inject Console console;
|
||||
|
||||
Exposed expo = new Exposed();
|
||||
console.print($"before: {expo}");
|
||||
|
||||
// you can only access public members from the default reference
|
||||
expo.pub = $"this was {expo.pub}";
|
||||
// expo.pro = $"this was {expo.pro}"; <- compiler error
|
||||
// expo.pri = $"this was {expo.pri}"; <- compiler error
|
||||
|
||||
// but you can ask for the protected reference ...
|
||||
assert (protected Exposed) expoPro := &expo.revealAs((protected Exposed));
|
||||
expoPro.pro = $"this was {expoPro.pro}";
|
||||
// expoPro.pri = $"this was {expoPro.pri}"; <- compiler error
|
||||
|
||||
// and you can ask for the private reference ...
|
||||
assert (private Exposed) expoPri := &expo.revealAs((private Exposed));
|
||||
expoPri.pri = $"this was {expoPri.pri}";
|
||||
|
||||
// or you can ask for the underlying struct, which is a passive
|
||||
// object that contains only the field storage
|
||||
assert (struct Exposed) expoStr := &expo.revealAs((struct Exposed));
|
||||
expoStr.pub = $"{expoStr.pub}!!!";
|
||||
expoStr.pro = $"{expoStr.pro}!!!";
|
||||
expoStr.pri = $"{expoStr.pri}!!!";
|
||||
|
||||
console.print($"after: {expo}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue