| 
Lua 基础教学:第二十五篇面向对象编程高级特性 在本篇文章中,我们将探讨 Lua 中面向对象编程的高级特性。尽管 Lua 本身并不直接支持面向对象编程(OOP),但通过表和元表,我们可以实现强大的面向对象编程特性。 回顾基础在之前的教程中,我们介绍了如何创建简单的类和对象,并实现基本的继承和多态。现在我们将深入探讨如何实现更复杂的 OOP 特性。 类和继承我们首先回顾一下如何实现类和继承。 基础类定义local Person = {} 
Person.__index = Person 
 
function Person:new(name, age) 
    local instance = setmetatable({}, Person) 
    instance.name = name 
    instance.age = age 
    return instance 
end 
 
function Person:greet() 
    print("Hello, my name is " .. self.name) 
end 
 
local person1 = Person:new("Alice", 30) 
person1:greet()  -- 输出:Hello, my name is Alice继承通过元表,我们可以实现继承。 local Student = setmetatable({}, Person) 
Student.__index = Student 
 
function Student:new(name, age, grade) 
    local instance = Person.new(self, name, age) 
    setmetatable(instance, Student) 
    instance.grade = grade 
    return instance 
end 
 
function Student:study() 
    print(self.name .. " is studying in grade " .. self.grade) 
end 
 
local student1 = Student:new("Bob", 20, "Sophomore") 
student1:greet()  -- 输出:Hello, my name is Bob 
student1:study()  -- 输出:Bob is studying in grade Sophomore多重继承Lua 允许通过多个元表实现多重继承,但需要手动处理冲突。 local function createClass(...) 
    local bases = {...} 
    local class = {} 
    class.__index = class 
 
    setmetatable(class, { 
        __index = function(_, key) 
            for _, base in ipairs(bases) do 
                if base[key] then 
                    return base[key] 
                end 
            end 
        end 
    }) 
 
    function class:new(...) 
        local instance = setmetatable({}, self) 
        if instance.init then 
            instance:init(...) 
        end 
        return instance 
    end 
 
    return class 
end 
 
local Athlete = {} 
function Athlete:train() 
    print(self.name .. " is training") 
end 
 
local Scholar = {} 
function Scholar:study() 
    print(self.name .. " is studying") 
end 
 
local StudentAthlete = createClass(Person, Athlete, Scholar) 
 
function StudentAthlete:init(name, age, sport, major) 
    Person.new(self, name, age) 
    self.sport = sport 
    self.major = major 
end 
 
local studentAthlete = StudentAthlete:new("Charlie", 22, "Basketball", "Physics") 
studentAthlete:greet()   -- 输出:Hello, my name is Charlie 
studentAthlete:train()   -- 输出:Charlie is training 
studentAthlete:study()   -- 输出:Charlie is studying封装与私有成员虽然 Lua 没有直接支持私有成员,但可以通过闭包实现封装。 local function createPerson(name, age) 
    local self = {} 
    local private = {name = name, age = age} 
 
    function self.getName() 
        return private.name 
    end 
 
    function self.setName(newName) 
        private.name = newName 
    end 
 
    function self.getAge() 
        return private.age 
    end 
 
    function self.greet() 
        print("Hello, my name is " .. private.name) 
    end 
 
    return self 
end 
 
local person2 = createPerson("Dave", 40) 
person2.greet()  -- 输出:Hello, my name is Dave 
print(person2.getAge())  -- 输出:40 
person2.setName("David") 
person2.greet()  -- 输出:Hello, my name is David多态与接口通过元方法,可以实现多态和接口。 local function createShape() 
    local shape = {} 
    shape.__index = shape 
 
    function shape:area() 
        error("This method should be overridden") 
    end 
 
    return shape 
end 
 
local Rectangle = setmetatable({}, createShape()) 
Rectangle.__index = Rectangle 
 
function Rectangle:new(width, height) 
    local instance = setmetatable({}, self) 
    instance.width = width 
    instance.height = height 
    return instance 
end 
 
function Rectangle:area() 
    return self.width * self.height 
end 
 
local Circle = setmetatable({}, createShape()) 
Circle.__index = Circle 
 
function Circle:new(radius) 
    local instance = setmetatable({}, self) 
    instance.radius = radius 
    return instance 
end 
 
function Circle:area() 
    return math.pi * self.radius ^ 2 
end 
 
local shapes = {Rectangle:new(4, 5), Circle:new(3)} 
for _, shape in ipairs(shapes) do 
    print("Area: " .. shape:area()) 
end总结在这篇教程中,我们探讨了 Lua 中面向对象编程的高级特性。我们学习了如何实现多重继承、封装与私有成员,以及多态与接口。通过这些技术,可以编写更复杂和灵活的面向对象代码。在接下来的教程中,我们将探讨 Lua 的内存管理和垃圾回收机制。 继续关注我们的 Lua 教程系列,如果你有任何问题或建议,请在评论区留言。 Happy Coding!  
 |