前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lua面向对象:new,继承,多态

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

作者头像
立羽
发布2023-08-24 15:09:52
3470
发布2023-08-24 15:09:52
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发

概述: 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
代码语言:javascript
复制
--保存类类型的虚表
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档