forked from solstice/solstice
private and protected fields
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
struct Person {
|
||||
name = ""
|
||||
age = 0
|
||||
protected name = ""
|
||||
private age = 0
|
||||
|
||||
def greet() string {
|
||||
return "Hello, " + self.name + "!"
|
||||
}
|
||||
|
||||
def dance() string {
|
||||
return "Dancing..."
|
||||
}
|
||||
|
||||
constructor(string name, int age) {
|
||||
self.name = name
|
||||
self.age = age
|
||||
@@ -20,3 +25,8 @@ max = Person("Max", 16)
|
||||
|
||||
puts max
|
||||
puts max.greet()
|
||||
puts max.name
|
||||
// puts max.age (causes compile time error, age is private)
|
||||
// max.name = "Maximilian" (causes compile time error, name is protected)
|
||||
|
||||
max.dance()
|
||||
|
||||
Reference in New Issue
Block a user