Skip to content

Commit 72e26a7

Browse files
committed
update README
1 parent 13c22ff commit 72e26a7

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

README.md

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ class 'Animal' {
4747
end
4848
}
4949

50-
class 'Dog' (extends 'Animal' {
50+
class 'Dog' : extends 'Animal' {
5151
constructor = function(self, name)
5252
self.name = name
5353
end
54-
})
54+
}
5555

5656
local dog = new 'Dog'('Bob')
5757
dog:eat() -- Eating...
@@ -74,61 +74,69 @@ class 'Animal' {
7474
end
7575
}
7676

77-
class 'Dog' (extends 'Animal' {
77+
class 'Dog' : extends 'Animal' {
7878
constructor = function(self, name)
7979
self.name = name
8080
end,
8181

8282
speak = function(self)
8383
print('Woof! My name is ' .. self.name .. '.')
8484
end
85-
})
85+
}
8686
```
8787

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.
8891

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:
9093

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:
92102

93103
```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 .. '...')
96115
end,
97116

98-
add = function(self, ...)
99-
self.overload ({
100-
[2] = function(self, a, b)
101-
return a + b
102-
end,
103-
[3] = function(self, a, b, c)
104-
return a + b + c
105-
end
106-
}, ...)
117+
receiveSMS = function(self, number, message)
118+
print('Received SMS from ' .. number .. ' to ' .. self.number .. '...')
107119
end
108120
}
109-
110-
local calc = new 'Calculator'()
111-
calc:add(1, 2) -- 3
112-
calc:add(1, 2, 3) -- 6
113121
```
114122

115-
## Interfaces
123+
### Multiple interfaces
116124

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:
120126

121127
```lua
122128
interface 'telePhone' {
123129
'call',
124130
'sendSMS',
125131
'receiveSMS'
126132
}
127-
```
128-
To make a class implement an interface, simply pass the interface name as an argument to the `implements` function. For example:
129133

130-
```lua
131-
class 'SmartPhone' (implements 'telePhone' {
134+
interface 'smartPhone' {
135+
'touchScreen',
136+
'internet'
137+
}
138+
139+
class 'SmartPhone' : implements ('telePhone', 'smartPhone') {
132140
constructor = function(self, number)
133141
self.number = number
134142
end,
@@ -143,8 +151,16 @@ class 'SmartPhone' (implements 'telePhone' {
143151

144152
receiveSMS = function(self, number, message)
145153
print('Received SMS from ' .. number .. ' to ' .. self.number .. '...')
154+
end,
155+
156+
touchScreen = function(self)
157+
print('Touching screen...')
158+
end,
159+
160+
internet = function(self)
161+
print('Accessing internet...')
146162
end
147-
})
163+
}
148164
```
149165

150166
## Inheritance of interfaces
@@ -158,10 +174,10 @@ interface 'telePhone' {
158174
'receiveSMS'
159175
}
160176

161-
interface 'smartPhone' (extends 'telePhone' {
177+
interface 'smartPhone' : extends 'telePhone' {
162178
'touchScreen',
163179
'internet'
164-
})
180+
}
165181
```
166182

167183
## instanceof

0 commit comments

Comments
 (0)