曲阜网站建设百度开户,做源码网站违法吗,网站建设与管理课程介绍,做招聘网站用什么代码开发文章目录1、基础介绍2、稍微深入的了解2.1 定义的函数的示例2.2 定义的函数的示例1、基础介绍
Car {NameBYD, Price12}print(Car 的地址 ---, Car)print(Car.Name) -- 访问属性
-- print(Car:Name) 报错print(string.rep(*, 20))fun…文章目录1、基础介绍2、稍微深入的了解2.1 定义的函数的示例2.2 定义的函数的示例1、基础介绍Car{NameBYD,Price12}print(Car 的地址 ---,Car)print(Car.Name)-- 访问属性-- print(Car:Name) 报错print(string.rep(*,20))functionCar:find1()print(: 定义的函数并且使用 : 调用时 self 的地址---,self)endfunctionCar.find2()print(. 定义的函数并且使用 . 调用时 self 的地址---,self)endCar:find1()Car.find2()print(string.rep(*,20))functionCar:find3()print(: 定义的函数并且使用 . 调用时 self 的地址---,self)endfunctionCar.find4()print(. 定义的函数并且使用 : 调用时 self 的地址---,self)endCar.find3()Car:find4()运行结果Car 的地址--- table: 038F9CA0BYD********************:定义的函数并且使用:调用时 self 的地址--- table: 038F9CA0.定义的函数并且使用.调用时 self 的地址--- nil********************:定义的函数并且使用.调用时 self 的地址--- nil.定义的函数并且使用:调用时 self 的地址--- nilCar{NameBYD,Price12}print(Car 的地址 ---,Car)print(Car.Name)-- 访问属性-- print(Car:Name) 报错print(string.rep(*,20))functionCar:find1(self)print(: 定义的函数并且使用 : 调用时 self 的地址---,self)endfunctionCar.find2(self)print(. 定义的函数并且使用 . 调用时 self 的地址---,self)endCar:find1()Car.find2()print(string.rep(*,20))functionCar:find3(self)print(: 定义的函数并且使用 . 调用时 self 的地址---,self)endfunctionCar.find4(self)print(. 定义的函数并且使用 : 调用时 self 的地址---,self)endCar.find3()Car:find4()运行结果Car 的地址--- table: 039D96E0BYD********************:定义的函数并且使用:调用时 self 的地址--- nil.定义的函数并且使用.调用时 self 的地址--- nil********************:定义的函数并且使用.调用时 self 的地址--- nil.定义的函数并且使用:调用时 self 的地址--- table: 039D96E0从上面我们知道使用点号.来访问 table 的属性不能使用:来访问 table 的属性。使用.和:都可以用来访问 table 的函数。使用:调用使用的.定义的函数默认函数传入的第一个参数是 table 本身。使用:定义的函数并且使用:调用时定义的函数中默认有一个变量self。而不是使用:定义的函数并且使用:调用时定义的函数中的是没有self变量的, 这个时候是不需要显示传入 self 参数的。2、稍微深入的了解通过上面的介绍我们对.和:有一个简单的了解接下我们再稍微深入的了解下。这里先说结论然后再看例子。.和:的区别在于使用 :使用:定义的函数时函数隐含 self 参数使用:调用函数会自动传入 table 至 self 参数。而使用.定义的函数并没有这样。Lua 中使用:可以实现面向对象方式的调用。:只是语法糖它同时在方法的声明与实现中增加了一个名为 self 的隐藏参数这个参数就是对象本身。2.1 定义的函数的示例classA{}print(ClassA 地址 --,classA)functionclassA:getob(name)print(classA:getob 中self 的地址 --,self)ob{}setmetatable(ob,self)self.__indexself self.namenamereturnobendfunctionclassA:getself()returnselfendc1classA:getob(A)print(c1 地址 --,c1)c2classA:getob(B)print(c2 地址 --,c2)print(string.rep(*,30))print(c1:getself())print(c2:getself())print(string.rep(*,30))----------------------继承------------------------classBclassA:getob()----非常重要用于获取继承的selffunctionclassB:getob(name,address)obclassA:getob(name)setmetatable(ob,self)self.__indexself self.addressaddressreturnobendc3classB:getob(gray.yang,shenzhen)print(c3 地址 --,c3)print(c3:getself())运行结果ClassA 地址-- table: 0392D128classA:getob 中self 的地址-- table: 0392D128c1 地址-- table: 0392D010classA:getob 中self 的地址-- table: 0392D128c2 地址-- table: 0392CE80******************************table:0392D010 table:0392CE80******************************classA:getob 中self 的地址-- table: 0392D128classA:getob 中self 的地址-- table: 0392D128c3 地址-- table: 03933050table:039330502.2 定义的函数的示例classA{}print(classA)functionclassA.new(cls,...)--定义类方法时使用.号不适用隐式传参print(cls)this{}setmetatable(this,cls)cls.__indexcls--将元表的__index设为自身访问表的属性不存在时会搜索元表cls.init(this,...)--初始化表注意访问类的方法都是.此时不会隐式传入参数returnthisendfunctionclassA.init(self,name)print(classA.init --,self)self.namenameendfunctionclassA.getname(self)print(classA.getname --,self)returnself.nameend-- 注意这里的调用方式是 : 。pclassA:new(gray.yang)print(p ---,p)print(p:getname())print(string.rep(*,50))运行结果table:0381B888 table:0381B888 classA.init-- table: 0381B8D8p--- table: 0381B8D8classA.getname-- table: 0381B8D8gray.yang**************************************************