• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

LUA对象

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

 

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);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua数据类型发布时间:2022-07-22
下一篇:
【转】Lua脚本语言入门发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap