在Lua中,是使用功能与QUOT之间的差异;:"和功能不? [英] In Lua, what is the difference between functions that use ":" and functions that do not?

查看:107
本文介绍了在Lua中,是使用功能与QUOT之间的差异;:"和功能不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我们有两个函数声明:

Let's say we have two function declarations:

function MyData:New 
end

function New(MyData)
end

它们之间有什么区别?是否使用有什么特殊的目的,当涉及到继承和OOP?通过使用 :我只能打电话与声明的函数

What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using :?

我是从仅使用C#来了 - 所以如果有需要做任何比较,那会是什么

I'm coming from using only C# -- so if there's any comparison to be made, what would it be?

推荐答案

因为有这个许多问题,但你有一组问题,所以我不能说这是一个重复的,您应该SO搜索。

You should search SO as there are many questions about this but you have a set of questions so I can't say this is a duplicate.

Q值。它们之间有什么区别?
A.与结肠该一个导致要加入到迈德特表的方法,以及所述的Lua间preTER自动插入时调用一个自我的第一个参数之前,用这种自我指向迈德特实例该办法正在呼吁。这是一样的书写 MyData.New =功能(个体经营)结束。第二个签名有一个名为迈德特参数,是一个全球性的功能。它是无关的迈德特表(或类)。

Q. What is the difference between them? A. The one with colon causes a method to be added to the MyData table, and the Lua interpreter to automatically insert a "self" before the first parameter when called, with this "self" pointing to the MyData instance that the "method" is being called upon. It is the same as writing MyData.New = function(self) end. The second signature has a parameter called MyData and is a global function. It is unrelated to the MyData table (or class).

Q值。是否使用:有什么特别的目的,当涉及到继承和OOP?
答:没有;它只不过是语法糖,这样当你调用 MyData.New ,你可以只写迈德特:新建()而不是笨重的期待 MyData.New(迈德特)。请注意,一个新功能通常是创建一个类的实例,所以你不会有这样的功能是一种方法,而只是在MyData的表的​​功能。对于传承与OOP,你用元表,而这并不能交互:任何特殊方式。

Q. Does using ":" have any special purpose when it comes to inheritance and OOP? A. No; it is merely syntactic sugar so that when you call MyData.New you can just write MyData:New() instead of the clunky looking MyData.New(MyData). Note that a "new" function is typically to create instances of a class, so you wouldn't have such a function be a method, rather just a function in the MyData table. For inheritence and OOP, you use metatables, and this does not interact with : in any special way.

Q值。我只能打电话与声明的函数:使用:?
答:不,如前所述,它只是语法糖,可以定义一个方法,并调用不同的方式。

Q. Can I only call functions declared with ":" by using ":"? A. No, as mentioned, it just syntactic sugar, you can define one way and call a different way.

Q值。我是从仅使用C#来了 - 所以如果有需要做任何比较,那会是什么?
答:对于功能的:就像。在C#,无论是在一个呼叫或定义中使用。在。在Lua更像是属性,出现在C#中的函数没有等价。

Q. I'm coming from using only C# -- so if there's any comparison to be made, what would it be? A. For functions, the : is like the . in C#, whether used in a call or definition. The "." in Lua is more like "attribute", there is no equivalent in C# for functions.

MyData = {} -- a table 
function MyData.func(self) 
    print('hello')
end
MyData:func()
MyData.func(MyData) -- same as previous

function MyData:func2() -- self is implicit
    print('hello')
end
MyData:func2()
MyData.func2(MyData) -- same as previous

注意迈德特如上定义是不是一类,因为你不能创建实例它(不这样做不存在显示额外的工作)。详细阅读在Lua网上图书编程的Lua.org网站上,大量的这些概念的有益的讨论。

Note that MyData as defined above is not a class, because you cannot create "instances" of it (without doing extra work not shown there). Definitely read the Programming in Lua online book on the Lua.org website, lots of useful discussions of these notions.

这篇关于在Lua中,是使用功能与QUOT之间的差异;:"和功能不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆