LUA对象

 

 

 

复制代码
Rectangle = {width = 0, height = 0, area = 0};

function Rectangle:new(o, width, height)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;
    
    self.width = width or 0;
    self.height = height or 0;

    self.area = width * height;

    return o;
end

function Rectangle:Area()
    print("矩形面积:" .. self.area);
end


r = Rectangle:new(nil, 10, 20);
print(r.width);
r:Area();
复制代码

 

 

复制代码
Car = {};

function Car:new(o)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;

    return o;

end

function Car:Driver()
     print("car can driver");
end

BMW = Car:new(nil);

function BMW:new(o)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;

    return o;
end

--此处重写了父类Car类的Driver方法
function BMW:Driver()
     print("BMW car can driver");
end



print("代码运行:");
c = Car:new(nil);
c.Driver();

print("BMW继承Car");
bmw = BMW:new();
bmw.Driver();
复制代码

 

复制代码
Car = {};

function Car:new(o)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;

    return o;

end

function Car:Driver()
     print("car can driver");
end

function Car:Oil()
    print("I need Oil");
end

BMW = Car:new(nil);

function BMW:new()
    o = o or {};
    setmetatable(o, self);
    self.__index = self;

    return o;
end

--此处重写了父类Car类的Driver方法
function BMW:Driver()
     print("重写Driver:BMW car can driver");
end

function BMW:Price()
     print("BMW cost 10000");
end


print("-------代码运行-------");
c = Car:new();
c.Driver();
c.Oil();

print("-------BMW继承Car-------");
bmw = BMW:new();
bmw.Driver();

bmw:Oil();
bmw:Price();    --调用类中方法可用:或.来调用
复制代码

 

 base.lua脚本

复制代码
Base = {};

function Base:new(o)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;
    return o;
end


function Base:Common()
    print("Base common function");
end


function hello()
    print("hello");
end
复制代码

 

 

复制代码
require("base");

hello();

b = Base:new(nil);

b.Common();


Lua8 = Base:new(nil);

function Lua8:new(o)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;
    return o;
end

function Lua8:Common()
    print("重写Base的Common方法");
end

l = Lua8:new(nil);
l:Common();
复制代码

 

 

 

复制代码
Student = {};
Student.__index = Student;

function Student:new(name, age)
    local temp = {};
    setmetatable(temp, Student);
    self.name = name;
    self.age = age;
    return temp;
end


function Student:info()
    print("name:" .. self.name .. " age:" .. self.age);
end

stu = Student:new("zhangsan", 10);
stu:info(stu);
复制代码

 

复制代码
Student = {};

function Student:new(o, name, age)
    o = o or {};
    setmetatable(o, self);
    self.__index = self;
    self.name = name;
    self.age = age;
    return o;
end


function Student:info()
    print("name:" .. self.name .. " age:" .. self.age);
end

stu = Student:new(nil, "zhangsan", 10);
stu:info(stu);
复制代码

 

posted @   Answer.AI.L  阅读(415)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示