lua面向对象:new,继承,多态

概述:
lua要实现类,继承,多态

  1. BaseClass(super)为生成基类公共方法,内部开辟新的以class_type为模板返回,主要为类的声明服务,一个类有的__init(构造),__delete(析构),.super(父类),.New(创建对象),setmetatable(class_type, {__index = _class[super]})设置元表,__index指向父类
  2. 类的声明BaseView = BaseView or BaseClass()
  3. 类的继承LoginView = LoginView or BaseClass(BaseView)
  4. 通过设置class_type.New,会再返回一个新的设置子类的元表obj,
  5. New方法里,setmetatable(obj, { __index = _class[class_type] }),把obj与class_type联系起来,并通过递归调用create,依次调用父类__init,子类__init。再注册DeleteMe方法,依次调用子类__delete,父类__delete
--保存类类型的虚表
local _class = {}
local lua_obj_count = 0

function BaseClass(super)
	-- 生成一个类类型
	local class_type = {}
	-- 在创建对象的时候自动调用
	class_type.__init = false
	class_type.__delete = false
	class_type.super = super
	class_type.New = function(...)
		lua_obj_count = lua_obj_count + 1
		-- 生成一个类对象
		local obj = {_class_type = class_type} --原始表,表里有个key为_class_type的value为刚定义的class_type

		-- 在初始化之前注册基类方法
		setmetatable(obj, { __index = _class[class_type] })

		-- 调用初始化方法
		--如果有基类,相当于用递归,把所有基类的init方法都调用了一遍,,而且是先调用最基类init,再子类的init
		do
			local create
			create = function(c, ...)
				if c.super then
					create(c.super, ...)
				end
				if c.__init then
					c.__init(obj, ...)
				end
			end

			create(class_type, ...)
		end

		-- 注册一个delete方法
		--先调用自己的__delete,再调用基类的__delete
		obj.DeleteMe = function(self)
			lua_obj_count = lua_obj_count - 1
			local now_super = self._class_type
			while now_super ~= nil do
				if now_super.__delete then
					now_super.__delete(self)
				end
				now_super = now_super.super
			end


		end


		return obj
	end

	--[[
	local vtbl = {}
	_class[class_type] = vtbl

	setmetatable(class_type, {__newindex = function(t,k,v) vtbl[k] = v end,
		__index = vtbl
	})

	if super then
		setmetatable(vtbl, {__index = function(t,k) return _class[super][k] end})
	end
--]]


	--记录下创建了多少表
	_class[class_type]  = class_type

	--把父类作为子类的元表
	if super then
		setmetatable(class_type, {__index = _class[super]})
	end


	return class_type
end

BaseView = BaseView or BaseClass()

BaseView.a = 1
BaseView.b = 2

function BaseView:__init()
	print("BaseView:__init")
end

function BaseView:__delete()
	print("BaseView:__delete")
end

function BaseView:DoBaseFunc()
	print("BaseView:DoBaseFunc")
end

function BaseView:DoSomething()
	print("BaseView:DoSomething")
end

LoginView = LoginView or BaseClass(BaseView)


function LoginView:__init()
	print("LoginView:__init")
end

function LoginView:__delete()
	print("LoginView:__delete")
end

function LoginView:DoSomething()
	print("LoginView:DoSomething")
end

ologin_view = LoginView:New() --new 一个对象
ologin_view:DeleteMe()

print(ologin_view.a)
ologin_view.a = 3
print(ologin_view.a)
ologin_view.c = 4
print(ologin_view.c)
print(BaseView.c)
print(ologin_view:DoBaseFunc())
print(ologin_view:DoSomething())



输出

BaseView:__init
LoginView:__init
LoginView:__delete
BaseView:__delete
1
3
4
nil
BaseView:DoBaseFunc
LoginView:DoSomething

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值