forked from Mirror/wren
docs; encapsulation legibility fixes
This commit is contained in:
@ -371,8 +371,8 @@ value is `null`.
|
||||
All fields are *private* in Wren—an object's fields can only be directly
|
||||
accessed from within methods defined on the object's class.
|
||||
|
||||
In short, if you want to make a property of an object visible, **you need to define a
|
||||
getter to expose it**:
|
||||
In short, if you want to make a property of an object visible,
|
||||
**you need to define a getter to expose it**:
|
||||
|
||||
:::wren
|
||||
class Rectangle {
|
||||
@ -382,7 +382,8 @@ getter to expose it**:
|
||||
// ...
|
||||
}
|
||||
|
||||
To allow outside code to modify the field, **you need to provide setters to provide access**:
|
||||
To allow outside code to modify the field,
|
||||
**you need to provide setters to provide access**:
|
||||
|
||||
:::wren
|
||||
class Rectangle {
|
||||
@ -392,8 +393,8 @@ To allow outside code to modify the field, **you need to provide setters to prov
|
||||
|
||||
This might be different from what you're used to, so here are two important facts:
|
||||
|
||||
- You can't access fields on another instance of your own class (unlike C++ and Java).
|
||||
- You can't access fields from a base class.
|
||||
- You can't access fields on another instance of your own class.
|
||||
|
||||
Here is an example in code:
|
||||
|
||||
@ -406,14 +407,17 @@ Here is an example in code:
|
||||
|
||||
class Rectangle is Shape {
|
||||
construct new() {
|
||||
//This will print null! _shape from the parent class is private,
|
||||
//so we are really just reading `_shape` from `this`, which has not been set.
|
||||
//This will print null!
|
||||
//_shape from the parent class is private,
|
||||
//we are reading `_shape` from `this`,
|
||||
//which has not been set, so returns null.
|
||||
System.print("I am a %(_shape)")
|
||||
|
||||
//a local variable, all variables are private
|
||||
_width = 10
|
||||
var other = Rectangle.new()
|
||||
//other._width is not accessible from here,
|
||||
|
||||
//other._width is not accessible from here,
|
||||
//even though we are also a rectangle. The field
|
||||
//is private, and other._width is invalid syntax!
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user