LUA对象
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对象的更多相关文章
- 传Lua对象到Cpp
传Lua对象到Cpp (金庆的专栏) 摘自:http://raycast.net/lua-intf 以下代码演示了Lua函数和表传入Cpp进行处理: std::string acceptStuff(L ...
- xlua中lua对象到c#对象的转型
lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...
- 开源基于lua gc管理c++对象的cocos2dx lua绑定方案
cocos2dx目前lua对应的c++对象的生命周期管理,是基于c++析构函数的,也就是生命周期可能存在不一致,比如c++对象已经释放,而lua对象还存在,如果这时候再使用,会有宕机的风险,为此我开发 ...
- Lua 之面向对象编程
Lua 之面向对象编程 Lua并不是为面向对象而设计的一种语言,因此,仅从原生态语法上并不直接支持面向对象编程,但Lua的设计中仍然包含了很多面向对象的思想,理解它们也更有助于理解Lua自身的一些高级 ...
- Lua 之数据结构
Lua 之数据结构 数组 通过整数下标访问的table中的元素,即是数组,下标默认从1开始. 一个创建二维数组的例子: mt = {} , do mt[i] = {} , do mt[i][j] = ...
- C++实现对lua访问的封装
这是一个几年前写的对lua的访问封装,当时的项目仅提供了最基本的lua访问接口:调用lua函数,向lua注册标准格式的C++函数. 本来我想引进luabind,但luabind相对又过于复杂,并不是所 ...
- LUA表克隆方法归纳
lua表克隆 将lua一个表, 克隆出一份为一个独立的另外一个表. 对于一个module, 如果在require之后,获得的表对象, 不能直接修改, 例如lua缓存此表, 但是多次逻辑执行, 都使用的 ...
- cocos2d-x lua绑定解析
花了几天时间看了下cocos2d-x lua绑定那块,总算是基本搞明白了,下面分三部分解析lua绑定: 一.lua绑定主要用到的底层函数 lua绑定其本质就是有一个公用的lua_Stack来进行C和L ...
- Lua 5.1 参考手册
Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes 云风 译 www.codingno ...
随机推荐
- Inno Setup新建项目
一.准备一个例子工程WEI 运行起来是这样的 二.开始新建 使用Inno Setup Compiler或Inno Script Studio新建都可以,我这里先用Inno Setup Compiler ...
- Kali-linux破解操作系统用户密码
当忘记操作系统的密码或者攻击某台主机时,需要知道该系统中某个用户的用户名和密码.本节将分别介绍破解Windows和Linux用户密码. 8.6.1 破解Windows用户密码 Windows系统的用户 ...
- Dubbo实践(十二)Refer
Spring在启动Dubbo客户端应用时,会实例化ReferenceBean<T>并设置配置属性,然后调用ReferenceConfig中的get方法: public synchroniz ...
- centos7 安装拼音输入法
依次选择Applications->System Tools->setting->Regiin&Language,添加Chinese(Intelligent Pinyin) ...
- HP-Socket国产优秀socket通信组件推荐
来源:http://blog.csdn.net/clb929/article/details/51085983 * HP-Socket 官方网站:http://www.jessma.org * HP- ...
- 误卸载glibc类库导致系统崩溃解决方案
由于系统中没有yum环境需要编译安装redis,但系统中却没有安装gcc和gcc-c++,挂载本地镜像安装gcc和gcc-c++由于版本太高,由于一时疏忽误将系统的依赖库glibc使用rpm -e 命 ...
- iframe 框架 父页面与子界面之间的变量、函数、元素获取
1.获取页面上的iframe 1-1. document.getElementById('iframeId'): 1-2. window.frames[0].window.frames['frameN ...
- 随记181120Service Fabric问题
https://github.com/Azure/service-fabric-issues/issues/1056 不能启动node one /five 问题
- jdbc之Statement和Preparement
Jdbc DML 操作 Statement:静态SQL操作 每次操作都会将sql语句提交到数据库执行一次,性能比较低 // 1.加载驱动程序 Class.forName(driverName); // ...
- Hive(10)-文件存储格式
Hive支持的存储数据的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET 一. 列式存储和行式存储 左边为逻辑表,右边第一个为行式存储,第二个为列式存储 1. 行式存储 ...