对象如何在Lua中引用自身?

时间:2022-06-01 12:37:24

C# has this and VB has ME. What is the Lua equivalent?

C#有这个,VB有ME。什么是Lua等价物?

I am trying to reference the parent of the script class in Roblox.

我试图在Roblox中引用脚本类的父级。

4 个解决方案

#1


From the Lua documentation section 2.5.9, the self reference is usually self:

从Lua文档部分2.5.9开始,自引用通常是self:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

冒号语法用于定义方法,即具有隐式额外参数self的函数。因此,声明

function t.a.b.c:f (params) body end

is syntactic sugar for

是语法糖

t.a.b.c.f = function (self, params) body end

#2


As Greg pointed out already, the name you are looking for is self.

正如格雷格已经指出的那样,你所寻找的名字就是自我。

However, be aware that Lua is not an OOP language any more than it is a purely procedural or functional language. It simply provides all the low level mechanisms to implement an OOP design. One of the design principles has been expressed as to "provide mechanism, not policy". Because of that, there is no way to guarantee that the environment you are running in even uses inheritance, or that you could find a parent for any given object.

但是,请注意Lua不是OOP语言,而是纯粹的程序或功能语言。它只是提供了实现OOP设计的所有低级机制。其中一个设计原则被表达为“提供机制,而不是政策”。因此,无法保证您运行的环境甚至使用继承,或者您可以找到任何给定对象的父级。

It would be a good idea to review the sections of the Lua manual, Programming in Lua, and the Wiki that relate to OOP features:

查看Lua手册,Lua中的Programming以及与OOP功能相关的Wiki的部分是一个好主意:

  • Lua Manual, especially sections 2.5.8, 2.5.9 and 2.8.
  • Lua手册,特别是2.5.8,2.5.9和2.8节。

  • PiL Chapter 16, linked to the online copy of the first edition, which was written at the time of Lua 5.0. Read the online copy, but be aware that the current version of Lua is different enough that buying the 2nd edition is highly recommended.)
  • PiL第16章,链接到第一版的在线副本,该副本是在Lua 5.0时编写的。阅读在线副本,但请注意,当前版本的Lua与我们强烈建议购买第2版非常不同。)

  • Lua Wiki on OOP, especially the tutorial and the article on simple classes.
  • 关于OOP的Lua Wiki,特别是关于简单类的教程和文章。

#3


In Lua, you'll want the "self" value. However, you're using ROBLOX, which is sandboxed. Each script is run in it's own thread, and to reference the script, you'll need to use "script", i.e. script.Parent

在Lua中,你会想要“自我”的价值。但是,您使用的是ROBLOX,它是沙箱。每个脚本都在它自己的线程中运行,要引用脚本,你需要使用“script”,即script.Parent

#4


local Table = {}
Table.Var = "Testing"

function Table:Test()
print(self.Var)
end
Table:Test()

or

local Table = {}
Table.Var = "Testing"
function Table.Test(self)
print(self.Var)
end

Both function will do the same exact thing.

这两个函数都会做同样的事情。

--Edit--

That only work with tables. If you are trying to get the parent of the script you need to use script.Parent

这只适用于表格。如果您尝试获取脚本的父级,则需要使用script.Parent

--Note script.Parent would return where the script is located. If you add another parent, script.Parent.Parent, it would return the parent of the parent, and so on.

- 注意script.Parent将返回脚本所在的位置。如果添加另一个父脚本script.Parent.Parent,它将返回父项的父项,依此类推。

#1


From the Lua documentation section 2.5.9, the self reference is usually self:

从Lua文档部分2.5.9开始,自引用通常是self:

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement

冒号语法用于定义方法,即具有隐式额外参数self的函数。因此,声明

function t.a.b.c:f (params) body end

is syntactic sugar for

是语法糖

t.a.b.c.f = function (self, params) body end

#2


As Greg pointed out already, the name you are looking for is self.

正如格雷格已经指出的那样,你所寻找的名字就是自我。

However, be aware that Lua is not an OOP language any more than it is a purely procedural or functional language. It simply provides all the low level mechanisms to implement an OOP design. One of the design principles has been expressed as to "provide mechanism, not policy". Because of that, there is no way to guarantee that the environment you are running in even uses inheritance, or that you could find a parent for any given object.

但是,请注意Lua不是OOP语言,而是纯粹的程序或功能语言。它只是提供了实现OOP设计的所有低级机制。其中一个设计原则被表达为“提供机制,而不是政策”。因此,无法保证您运行的环境甚至使用继承,或者您可以找到任何给定对象的父级。

It would be a good idea to review the sections of the Lua manual, Programming in Lua, and the Wiki that relate to OOP features:

查看Lua手册,Lua中的Programming以及与OOP功能相关的Wiki的部分是一个好主意:

  • Lua Manual, especially sections 2.5.8, 2.5.9 and 2.8.
  • Lua手册,特别是2.5.8,2.5.9和2.8节。

  • PiL Chapter 16, linked to the online copy of the first edition, which was written at the time of Lua 5.0. Read the online copy, but be aware that the current version of Lua is different enough that buying the 2nd edition is highly recommended.)
  • PiL第16章,链接到第一版的在线副本,该副本是在Lua 5.0时编写的。阅读在线副本,但请注意,当前版本的Lua与我们强烈建议购买第2版非常不同。)

  • Lua Wiki on OOP, especially the tutorial and the article on simple classes.
  • 关于OOP的Lua Wiki,特别是关于简单类的教程和文章。

#3


In Lua, you'll want the "self" value. However, you're using ROBLOX, which is sandboxed. Each script is run in it's own thread, and to reference the script, you'll need to use "script", i.e. script.Parent

在Lua中,你会想要“自我”的价值。但是,您使用的是ROBLOX,它是沙箱。每个脚本都在它自己的线程中运行,要引用脚本,你需要使用“script”,即script.Parent

#4


local Table = {}
Table.Var = "Testing"

function Table:Test()
print(self.Var)
end
Table:Test()

or

local Table = {}
Table.Var = "Testing"
function Table.Test(self)
print(self.Var)
end

Both function will do the same exact thing.

这两个函数都会做同样的事情。

--Edit--

That only work with tables. If you are trying to get the parent of the script you need to use script.Parent

这只适用于表格。如果您尝试获取脚本的父级,则需要使用script.Parent

--Note script.Parent would return where the script is located. If you add another parent, script.Parent.Parent, it would return the parent of the parent, and so on.

- 注意script.Parent将返回脚本所在的位置。如果添加另一个父脚本script.Parent.Parent,它将返回父项的父项,依此类推。