You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-32Lines changed: 48 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,11 +47,11 @@ class 'Animal' {
47
47
end
48
48
}
49
49
50
-
class'Dog' (extends'Animal' {
50
+
class'Dog' : extends'Animal' {
51
51
constructor=function(self, name)
52
52
self.name=name
53
53
end
54
-
})
54
+
}
55
55
56
56
localdog=new'Dog'('Bob')
57
57
dog:eat() -- Eating...
@@ -74,61 +74,69 @@ class 'Animal' {
74
74
end
75
75
}
76
76
77
-
class'Dog' (extends'Animal' {
77
+
class'Dog' : extends'Animal' {
78
78
constructor=function(self, name)
79
79
self.name=name
80
80
end,
81
81
82
82
speak=function(self)
83
83
print('Woof! My name is ' ..self.name..'.')
84
84
end
85
-
})
85
+
}
86
86
```
87
87
88
+
## Interfaces
89
+
90
+
The **Lua Class** library als supports interface concepts. An interface is a blueprint of a class that defines the methods and attributes that a class must implement. A class can implement multiple interfaces and must implement all the methods and attributes defined in the interfaces it implements.
88
91
89
-
### Method overloading
92
+
To create an interface, simply call the `interface` function and pass the interface name and table containing the methods attributes of the interface as arguments. For example:
90
93
91
-
The **Lua Class** library also supports method overloading. This means that a class can have multiple methods with the same name, but with different parameters. To overload a method, simply call the `self.overload` function and pass a table containing the different implementations of the method as arguments, but the index of the table must be the number of parameters of the method. For example:
94
+
```lua
95
+
interface'telePhone' {
96
+
'call',
97
+
'sendSMS',
98
+
'receiveSMS'
99
+
}
100
+
```
101
+
To make a class implement an interface, simply pass the interface name as an argument to the `implements` function. For example:
92
102
93
103
```lua
94
-
class'Calculator' {
95
-
constructor=function(self)
104
+
class'SmartPhone' : implements'telePhone' {
105
+
constructor=function(self, number)
106
+
self.number=number
107
+
end,
108
+
109
+
call=function(self, number)
110
+
print('Calling ' ..number..' from ' ..self.number..'...')
111
+
end,
112
+
113
+
sendSMS=function(self, number, message)
114
+
print('Sending SMS to ' ..number..' from ' ..self.number..'...')
96
115
end,
97
116
98
-
add=function(self, ...)
99
-
self.overload ({
100
-
[2] =function(self, a, b)
101
-
returna+b
102
-
end,
103
-
[3] =function(self, a, b, c)
104
-
returna+b+c
105
-
end
106
-
}, ...)
117
+
receiveSMS=function(self, number, message)
118
+
print('Received SMS from ' ..number..' to ' ..self.number..'...')
107
119
end
108
120
}
109
-
110
-
localcalc=new'Calculator'()
111
-
calc:add(1, 2) -- 3
112
-
calc:add(1, 2, 3) -- 6
113
121
```
114
122
115
-
##Interfaces
123
+
### Multiple interfaces
116
124
117
-
The **Lua Class** library als supports interface concepts. An interface is a blueprint of a class that defines the methods and attributes that a class must implement. A class can implement multiple interfaces and must implement all the methods and attributes defined in the interfaces it implements.
118
-
119
-
To create an interface, simply call the `interface` function and pass the interface name and table containing the methods attributes of the interface as arguments. For example:
125
+
The **Lua Class** library also supports multiple interfaces. This means that a class can implement multiple interfaces. To make a class implement multiple interfaces, simply pass the interface names as arguments to the `implements` function. For example:
120
126
121
127
```lua
122
128
interface'telePhone' {
123
129
'call',
124
130
'sendSMS',
125
131
'receiveSMS'
126
132
}
127
-
```
128
-
To make a class implement an interface, simply pass the interface name as an argument to the `implements` function. For example:
0 commit comments