local function CreateRobot(name,id) local obj = {name = name,id = id} function obj:SetName(name) self.name = name end functionobj:GetName() returnself.name end functionobj:SetId(id) self.id = id end functionobj:GetId() returnself.id end return obj end local functioncreateFootballRobot(name ,id ,position) local obj = CreateRobot(name ,id) obj.position = "right back" functionobj:SetPosition(p) self.position = p end functionobj:GetPosition() returnself.position endreturn obj end local mycreateFootballRobot = createFootballRobot("Tom",1000,"广州") print("mycreateFootballRobot's name:",mycreateFootballRobot:GetName(),"myCreate's id:",mycreateFootballRobot:GetId(),mycreateFootballRobot:GetPosition()) mycreateFootballRobot:SetName("麦迪") mycreateFootballRobot:SetId(2000) mycreateFootballRobot:SetPosition("北京") print("mycreateFootballRobot's name:",mycreateFootballRobot:GetName(),"myCreate's id:",mycreateFootballRobot:GetId(),mycreateFootballRobot:GetPosition())
输出结果:
1 2 3
mycreateFootballRobot’s name:TommyCreate’s id:1000right back