Lua面向对象 --- 继承
工程结构:
BasePlayer.lua:
1 BasePlayer = {} 2 3 BasePlayer.root = "BasePlayer" 4 5 function BasePlayer:ShowBaseName() 6 print("ShowBaseName : BasePlayer") 7 end 8 9 return BasePlayer
Player.lua:
1 require "BasePlayer" 2 3 Player = {} 4 5 6 function Player:new() 7 setmetatable(Player,{__index = BasePlayer}) 8 9 self = {} 10 setmetatable(self,{__index = Player}) 11 return self 12 end 13 14 function Player:ShowPlayerName() 15 print("ShowPlayerName : Player") 16 end 17 18 return Player
Main.lua:
1 require "Player" 2 3 testPlayer = Player:new() 4 5 print(testPlayer.root) 6 testPlayer:ShowBaseName() 7 testPlayer:ShowPlayerName() 8 9 --[[ 10 运行结果: 11 BasePlayer 12 ShowBaseName : BasePlayer 13 ShowPlayerName : Player 14 --]]
码云上的相关工程:https://gitee.com/luguoshuai/LearnLua