在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Student = {}; Student.__index = Student; function Student:new(name, age) local temp = {}; setmetatable(temp, Student); temp.name = name; temp.age = age; return temp; end function Student:info() print(self.name, self.age);
Student = {}; Student.__index = Student; --此处做修改 function Student.new(name, age) local temp = {}; setmetatable(temp, Student); temp.name = name; temp.age = age; return temp; end function Student:info() print(self.name, self.age); end --输出:nil stu1 stu1 = Student.new(nil, "stu1", 10); stu1:info(); --输出:stu2 10 stu2 = Student.new("stu2", 10); stu2:info(); --输出:table: 0037B200 nil stu3 = Student:new(nil, "stu3", 10); stu3:info(); --输出:table: 0037B200 stu4 stu4 = Student:new("stu4", 10); stu4:info(); --输出:table: 0084B200 stu5 stu5 = Student:new("stu5"); stu5:info();
Student = {}; Student.__index = Student; function Student.new(name, age) local temp = {}; setmetatable(temp, Student); temp.name = name; temp.age = age; return temp; end function Student:info() print(self.name, self.age); end function Student:message() print(self.name, self.age); end function Student.school() print("go to school!!!"); end --声明类时用点号:Student.new(name, age) stu = Student:new("zhangsan", 10); stu:info(); --输出信息错误 stu.school(); --stu.message(); --报错 stu.message(stu);--输出信息错误 mes = Student.new("zhangsan", 10); --mes.info(); --报错 mes.info(mes); mes.school(); --声明类时用冒号:Student.new(name, age) --stu = Student:new("zhangsan", 10); --stu:info(); --stu.info(stu); --stu.school(); --mes = Student.new(nil, "mes", 20); --mes:message(); --mes.school();
|
请发表评论