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. @PostConstruct与@PreDestroy讲解及实例

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化后和销毁bean之前进行的操作 第 ...

  2. momenta

    编程:1.dp的题 2.白纸写代码,给一串数和一个目标值,使用四则运算和括号使得这串数最后计算结果为目标值,打印出所有的方案,要求是这些数每个最多被使用一次,可以不被用到. 3.考了一个查找数组里,未 ...

  3. 一种基于openflow的虚拟化层软件flowvisor的API测试

    注明:本文并不对openflow进行分析,本人也是略略知道这个概念,对flowvisor也只是对其API有所测试,更深的源码并未涉及,只是希望该文能对以后的flowvisor研究者提供些许帮助. 一: ...

  4. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  5. vlc源码分析(四) 调用libts接收TS流

    代码分析前,先要了解TS流基本概念:TS流之基本概念. VLC解析TS流是通过libts库来分离的,libts库使用libdvbpsi库来解TS表.VLC使用模块加载机制来加载libts库,具体调用的 ...

  6. Vue学习—组件的学习

    1.什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能 ...

  7. Apache 各启动方式的差别

    apachectl 调用 $httpd -k 1. kill - TERM `cat /usr/local/apache/logs/http.pid` 2. /bin/httpd -k  stop/s ...

  8. iOS 后台持续定位详解(支持ISO9.0以上)

    iOS 后台持续定位详解(支持ISO9.0以上) #import <CoreLocation/CoreLocation.h>并实现CLLocationManagerDelegate 代理, ...

  9. C# 自定义特性Attribute

    一.特性Attribute和注释有什么区别 特性Attribute A:就是一个类,直接继承/间接继承Attribute B:特性可以在后期反射中处理,特性本身是没有什么*用的 C:特性会影响编译和运 ...

  10. Java工具-----native2ascii

    概述 native2ascii.exe位于%JAVA_HOME/bin目录下,所以要使用,得先安装JDK. 该工具用来将本地编码转换为Unicode,英文字母.阿拉伯数字不会转化. 官方文档:http ...