1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
print("=== Basic Class Implementation ===")
Person = {}
function Person:new(name, age) local obj = {} setmetatable(obj, self) self.__index = self obj.name = name obj.age = age return obj end
function Person:greet() print("Hello, my name is " .. self.name .. " and I'm " .. self.age .. " years old.") end
function Person:set_age(new_age) self.age = new_age print(self.name .. "'s age is now " .. self.age) end
local person1 = Person:new("Alice", 25) local person2 = Person:new("Bob", 30)
person1:greet() person2:greet()
person1:set_age(26) person1:greet()
print("\n=== Inheritance Implementation ===")
Student = Person:new()
function Student:new(name, age, student_id, major) local obj = Person:new(name, age) setmetatable(obj, self) self.__index = self obj.student_id = student_id obj.major = major return obj end
function Student:greet() print("Hello, I'm " .. self.name .. ", a " .. self.major .. " student with ID " .. self.student_id .. ".") end
function Student:study(subject) print(self.name .. " is studying " .. subject .. ".") end
local student1 = Student:new("Charlie", 20, "S12345", "Computer Science") local student2 = Student:new("Diana", 21, "S67890", "Mathematics")
student1:greet() student1:study("Lua Programming") student1:set_age(21)
student2:greet() student2:study("Calculus")
print("\n=== Multiple Inheritance (Mixin Approach) ===")
CanSwim = { swim = function(self) print(self.name .. " is swimming.") end }
CanFly = { fly = function(self) print(self.name .. " is flying.") end }
Bird = Person:new()
function Bird:new(name, age, wingspan) local obj = Person:new(name, age) setmetatable(obj, self) self.__index = self for k, v in pairs(CanFly) do if not obj[k] then obj[k] = v end end obj.wingspan = wingspan return obj end
function Bird:greet() print("Chirp! I'm " .. self.name .. " with a " .. self.wingspan .. "cm wingspan.") end
Duck = Bird:new()
function Duck:new(name, age, wingspan) local obj = Bird:new(name, age, wingspan) setmetatable(obj, self) self.__index = self for k, v in pairs(CanSwim) do if not obj[k] then obj[k] = v end end return obj end
function Duck:greet() print("Quack! I'm " .. self.name .. ".") end
local duck = Duck:new("Donald", 5, 60) duck:greet() duck:fly() duck:swim() duck:set_age(6)
print("\n=== Private Property Simulation ===")
function create_private_person(name, age) local private_name = name local private_age = age local obj = {} obj.get_name = function() return private_name end obj.get_age = function() return private_age end obj.set_age = function(new_age) if new_age > 0 then private_age = new_age return true end return false end obj.greet = function() print("Hello, my name is " .. private_name .. " and I'm " .. private_age .. " years old.") end return obj end
local private_person = create_private_person("Eve", 35) private_person:greet() print("Name:", private_person.get_name()) print("Age:", private_person.get_age())
print("Can access private_name directly?", private_person.private_name)
private_person.set_age(36) private_person:greet()
local success = private_person.set_age(-5) print("Setting invalid age succeeded?", success)
print("\n=== Class Static Properties and Methods ===")
Counter = { count = 0, instances = {} }
function Counter:new() local obj = {} setmetatable(obj, self) self.__index = self self.count = self.count + 1 table.insert(self.instances, obj) return obj end
function Counter.get_total_instances() return Counter.count end
function Counter.reset() Counter.count = 0 Counter.instances = {} print("Counter reset.") end
local counter1 = Counter:new() local counter2 = Counter:new() local counter3 = Counter:new()
print("Total Counter instances:", Counter.get_total_instances()) print("Static count attribute:", Counter.count)
Counter.reset() print("Total Counter instances after reset:", Counter.get_total_instances())
print("\n=== Polymorphism Example ===")
Shape = {}
function Shape:new() local obj = {} setmetatable(obj, self) self.__index = self return obj end
function Shape:area() error("Abstract method 'area' must be implemented by subclass") end
Circle = Shape:new()
function Circle:new(radius) local obj = Shape:new() setmetatable(obj, self) self.__index = self obj.radius = radius return obj end
function Circle:area() return math.pi * self.radius * self.radius end
Rectangle = Shape:new()
function Rectangle:new(width, height) local obj = Shape:new() setmetatable(obj, self) self.__index = self obj.width = width obj.height = height return obj end
function Rectangle:area() return self.width * self.height end
Square = Rectangle:new()
function Square:new(side) local obj = Rectangle:new(side, side) setmetatable(obj, self) self.__index = self return obj end
function print_area(shape) print("Area:", shape:area()) end
local circle = Circle:new(5) local rectangle = Rectangle:new(4, 6) local square = Square:new(5)
print("Circle area:") print_area(circle)
print("Rectangle area:") print_area(rectangle)
print("Square area:") print_area(square)
|