Skip to content

Commit 61f44fa

Browse files
committed
Document protected fields
1 parent 492e7db commit 61f44fa

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

docs/New Features/Object-Oriented Programming.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,31 @@ print(human.name) --> John
128128

129129
Note that if you have a local variable (or function parameter) called "parent", the `parent` expression will defer to it.
130130

131-
## Private Fields
131+
## Restricted Fields
132132

133-
Pluto allows you to specify if a field is 'public' or 'private'. Private fields can only be accessed by the class that defined them.
133+
Pluto allows you to specify if a field is 'public', 'protected' or 'private'. Private fields can only be accessed by the class that defined them. Protected fields can additionally also be accessed by descendant classes.
134134

135135
```pluto
136-
class Human
137-
public name
138-
private age
136+
class Vehicle
137+
function __construct(protected manufacturer)
138+
end
139+
end
139140
140-
function __construct(name, age)
141-
self.name = name
142-
self.age = age
141+
class Car extends Vehicle
142+
function __construct(manufacturer, private model, public year)
143+
parent:__construct(manufacturer)
143144
end
144145
145-
function getAge()
146-
return self.age
146+
function getName()
147+
return $"{self.year} {self.manufacturer} {self.model}"
147148
end
148149
end
149150
150-
local human = new Human("John", 42)
151-
print(human.name) -- "John"
152-
print(human:getAge()) -- 42
153-
print(human.age) -- nil
151+
local cor = new Car("Toyota", "Corolla", 2025)
152+
print(cor.manufacturer) --> nil
153+
print(cor.model) --> nil
154+
print(cor.year) --> 2025
155+
print(cor:getName()) --> 2025 Toyota Corolla
154156
```
155157

156158
## Constructor Promotion

0 commit comments

Comments
 (0)