Rectangle = {width = , height = , area = };

function Rectangle:new(o, width, height)
o = o or {};
setmetatable(o, self);
self.__index = self; self.width = width or ;
self.height = height or ; self.area = width * height; return o;
end function Rectangle:Area()
print("矩形面积:" .. self.area);
end r = Rectangle:new(nil, , );
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", );
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", );
stu:info(stu);

LUA对象的更多相关文章

  1. 传Lua对象到Cpp

    传Lua对象到Cpp (金庆的专栏) 摘自:http://raycast.net/lua-intf 以下代码演示了Lua函数和表传入Cpp进行处理: std::string acceptStuff(L ...

  2. xlua中lua对象到c#对象的转型

    lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...

  3. 开源基于lua gc管理c++对象的cocos2dx lua绑定方案

    cocos2dx目前lua对应的c++对象的生命周期管理,是基于c++析构函数的,也就是生命周期可能存在不一致,比如c++对象已经释放,而lua对象还存在,如果这时候再使用,会有宕机的风险,为此我开发 ...

  4. Lua 之面向对象编程

    Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...

  5. Lua 之数据结构

    Lua 之数据结构 数组 通过整数下标访问的table中的元素,即是数组,下标默认从1开始. 一个创建二维数组的例子: mt = {} , do mt[i] = {} , do mt[i][j] = ...

  6. C++实现对lua访问的封装

    这是一个几年前写的对lua的访问封装,当时的项目仅提供了最基本的lua访问接口:调用lua函数,向lua注册标准格式的C++函数. 本来我想引进luabind,但luabind相对又过于复杂,并不是所 ...

  7. LUA表克隆方法归纳

    lua表克隆 将lua一个表, 克隆出一份为一个独立的另外一个表. 对于一个module, 如果在require之后,获得的表对象, 不能直接修改, 例如lua缓存此表, 但是多次逻辑执行, 都使用的 ...

  8. cocos2d-x lua绑定解析

    花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...

  9. Lua 5.1 参考手册

    Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingno ...

随机推荐

  1. fastjson反序列化TemplatesImpl

    环境参考第一个链接,直接用IDEA打开 编译EvilObject.java成EvilObject.class 先看poc,其中NASTY_CLASS为TemplatesImpl类,evilCode是E ...

  2. 2、Android-UI(RecyclerView)

    2.6.滚动控件-RecylerView ListView虽然使用的效果很好但是也是有缺点的 不使用一些技巧来提升它的运行效率,性能就非常差 扩展性也不是很好 只能实现数据的纵向滚动效果 实现横向滚动 ...

  3. Java泛型 PECS(Producer Extends, Consumer Super)

    本文转载自ImportNew,原文链接 Java 泛型: 什么是PECS(Producer Extends, Consumer Super) PECS指“Producer Extends,Consum ...

  4. PL/SQL Developer修改窗口字体和大小

    工具 → 首选项 → 字体 → 选择, 然后自己调节设置

  5. PAT——1038. 统计同成绩学生

    本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过105的正整数N,即学生总人数.随后1行给出N名学生的百分制整数成绩,中间以空格分隔.最后1行给出要查 ...

  6. 【luogu P2831 愤怒的小鸟】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2831 写点做题总结:dp,搜索,重在设计状态,状态设的好,转移起来也方便. 对于一条抛物线,三点确定.(0, ...

  7. Http请求发送json数据用实体类接收

    以上是请求URL以及json数据 接收层

  8. Vcenter虚拟化三部曲----Vcenter server 5.5安装部署

    配置SQL Server 2008 R2 1.选择启动 SQL Server Management Studio. 2.选择SQL Server 身份验证登录 ---- 输入sa用户及密码. 3.右键 ...

  9. BZOJ 5248: [2018多省省队联测]一双木棋(对抗搜索)

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 439  Solved: 379[Submit][Status][Discuss] Descriptio ...

  10. WebGl 利用缓冲区对象画多个点

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...