1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-06 06:47:54 +02:00
This commit is contained in:
CoolDark
2014-11-16 12:48:02 +03:00
parent 423d637382
commit a281e7d0ff

View File

@@ -267,13 +267,14 @@ eatenBy = myFavs.animal -- работает! спасибо, мета-табл
-- __call(a, ...) для a(...) -- __call(a, ...) для a(...)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- 3.2 Class-like tables and inheritance. -- 3.2 Классы и наследования.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Classes aren't built in; there are different ways to make them using -- В Lua нет поддержки классов на уровне языка;
-- tables and metatables. -- Однако существуют разные способы их создания с помощью
-- таблиц и метатаблиц.
-- Explanation for this example is below it. -- Пример классам находится ниже.
Dog = {} -- 1. Dog = {} -- 1.
@@ -290,23 +291,22 @@ end
mrDog = Dog:new() -- 7. mrDog = Dog:new() -- 7.
mrDog:makeSound() -- 'I say woof' -- 8. mrDog:makeSound() -- 'I say woof' -- 8.
-- 1. Dog acts like a class; it's really a table. -- 1. Dog похоже на класс; но это таблица.
-- 2. "function tablename:fn(...)" is the same as -- 2. "function tablename:fn(...)" как и
-- "function tablename.fn(self, ...)", The : just adds a first arg called -- "function tablename.fn(self, ...)", Просто : добавляет первый аргумент
-- self. Read 7 & 8 below for how self gets its value. -- перед собой. Читай 7 и 8 чтоб понять как self получает значение.
-- 3. newObj will be an instance of class Dog. -- 3. newObj это экземпляр класса Dog.
-- 4. "self" is the class being instantiated. Often self = Dog, but inheritance -- 4. "self" есть класс являющийся экземпляром. Зачастую self = Dog, но экземляр
-- can change it. newObj gets self's functions when we set both newObj's -- может поменять это. newObj получит свои функции, когда мы установим newObj как
-- metatable and self's __index to self. -- метатаблицу и __index на себя.
-- 5. Reminder: setmetatable returns its first arg. -- 5. Помни: setmetatable возвращает первый аргумент.
-- 6. The : works as in 2, but this time we expect self to be an instance -- 6. ":" Работает в 2 стороны, но в этот раз мы ожидмаем, что self будет экземпляром
-- instead of a class. -- а не классом.
-- 7. Same as Dog.new(Dog), so self = Dog in new(). -- 7. Dog.new(Dog), тоже самое что self = Dog in new().
-- 8. Same as mrDog.makeSound(mrDog); self = mrDog. -- 8. mrDog.makeSound(mrDog) будет self = mrDog.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Inheritance example: -- Пример наследования:
LoudDog = Dog:new() -- 1. LoudDog = Dog:new() -- 1.
@@ -319,17 +319,16 @@ seymour = LoudDog:new() -- 3.
seymour:makeSound() -- 'woof woof woof' -- 4. seymour:makeSound() -- 'woof woof woof' -- 4.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- 1. LoudDog gets Dog's methods and variables. -- 1. LoudDog получит методы и переменные класса Dog.
-- 2. self has a 'sound' key from new(), see 3. -- 2. self будет 'sound' ключ для new(), смотри 3й пункт.
-- 3. Same as "LoudDog.new(LoudDog)", and converted to "Dog.new(LoudDog)" as -- 3. Так же как "LoudDog.new(LoudDog)" и переделанный в "Dog.new(LoudDog)"
-- LoudDog has no 'new' key, but does have "__index = Dog" on its metatable. -- LoudDog не имеет ключ 'new', но может выполнить "__index = Dog" в этой метатаблице
-- Result: seymour's metatable is LoudDog, and "LoudDog.__index = Dog". So -- Результат: Метатаблица seymour стала LoudDog и "LoudDog.__index = Dog"
-- seymour.key will equal seymour.key, LoudDog.key, Dog.key, whichever -- Так же seymour.key будет равна seymour.key, LoudDog.key, Dog.key,
-- table is the first with the given key. -- в зависимости от того какая таблица будет с первым ключем.
-- 4. The 'makeSound' key is found in LoudDog; this is the same as -- 4. 'makeSound' ключ найден в LoudDog; и выглдяит как "LoudDog.makeSound(seymour)".
-- "LoudDog.makeSound(seymour)".
-- If needed, a subclass's new() is like the base's: -- При необходимости, подкласс new() будет базовым.
function LoudDog:new() function LoudDog:new()
local newObj = {} local newObj = {}
-- set up newObj -- set up newObj
@@ -342,12 +341,12 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
--[[ I'm commenting out this section so the rest of this script remains --[[ Я закомментировал этот раздел так как часть скрипта остается
-- runnable. -- работоспособной.
``` ```
```lua ```lua
-- Suppose the file mod.lua looks like this: -- Предположим файл mod.lua будет выглядеть так:
local M = {} local M = {}
local function sayMyName() local function sayMyName()
@@ -361,57 +360,55 @@ end
return M return M
-- Another file can use mod.lua's functionality: -- Иные файлы могут использовать функционал mod.lua:
local mod = require('mod') -- Run the file mod.lua. local mod = require('mod') -- Запустим файл mod.lua.
-- require is the standard way to include modules. -- require - подключает модули.
-- require acts like: (if not cached; see below) -- require выглядит так: (если не кешируется; смотри ниже)
local mod = (function () local mod = (function ()
<contents of mod.lua> <contents of mod.lua>
end)() end)()
-- It's like mod.lua is a function body, so that locals inside mod.lua are -- Тело функции mod.lua является локальным, поэтому
-- invisible outside it. -- содержимое не видимо за телом функции.
-- This works because mod here = M in mod.lua: -- Это работает так как mod здесь = M в mod.lua:
mod.sayHello() -- Says hello to Hrunkner. mod.sayHello() -- Скажет слово Hrunkner.
-- This is wrong; sayMyName only exists in mod.lua: -- Это будет ошибочным; sayMyName доступен только в mod.lua:
mod.sayMyName() -- error mod.sayMyName() -- ошибка
-- require's return values are cached so a file is run at most once, even when -- require возвращает значения кеша файла вызванного не более одного раза, даже когда
-- require'd many times. -- требуется много раз.
-- Suppose mod2.lua contains "print('Hi!')". -- Предположим mod2.lua содержит "print('Hi!')".
local a = require('mod2') -- Prints Hi! local a = require('mod2') -- Напишет Hi!
local b = require('mod2') -- Doesn't print; a=b. local b = require('mod2') -- Не напишет; a=b.
-- dofile is like require without caching: -- dofile работает без кэша:
dofile('mod2') --> Hi! dofile('mod2') --> Hi!
dofile('mod2') --> Hi! (runs again, unlike require) dofile('mod2') --> Hi! (напишет снова)
-- loadfile loads a lua file but doesn't run it yet. -- loadfile загружает lua файл, но не запускает его.
f = loadfile('mod2') -- Calling f() runs mod2.lua. f = loadfile('mod2') -- Вызовет f() запустит mod2.lua.
-- loadstring is loadfile for strings. -- loadstring это loadfile для строк.
g = loadstring('print(343)') -- Returns a function. g = loadstring('print(343)') -- Вернет функцию.
g() -- Prints out 343; nothing printed before now. g() -- Напишет 343.
--]] --]]
``` ```
## References ## Примечание (от автора)
I was excited to learn Lua so I could make games Я был взволнован, когда узнал что с Lua я могу делать игры при помощи <a href="http://love2d.org/">Love 2D game engine</a>. Вот почему.
with the <a href="http://love2d.org/">Love 2D game engine</a>. That's the why.
I started with <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV's Lua for programmers</a>. Я начинал с <a href="http://nova-fusion.com/2012/08/27/lua-for-programmers-part-1/">BlackBulletIV's Lua for programmers</a>.
Next I read the official <a href="http://www.lua.org/pil/contents.html">Programming in Lua</a> book. Затем я прочитал официальную <a href="http://www.lua.org/pil/contents.html">Документацию по Lua</a>.
That's the how.
It might be helpful to check out the <a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Lua short Так же может быть полезным<a href="http://lua-users.org/files/wiki_insecure/users/thomasl/luarefv51.pdf">Lua short
reference</a> on lua-users.org. reference</a> на lua-users.org.
The main topics not covered are standard libraries: Основные темы не охваченные стандартной библиотекой:
* <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a> * <a href="http://lua-users.org/wiki/StringLibraryTutorial">string library</a>
* <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a> * <a href="http://lua-users.org/wiki/TableLibraryTutorial">table library</a>
@@ -419,8 +416,9 @@ The main topics not covered are standard libraries:
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a> * <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a> * <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>
By the way, the entire file is valid Lua; save it Весь файл написан на Lua; сохрани его как learn.lua и запусти при помощи "lua learn.lua" !
as learn.lua and run it with "lua learn.lua" !
Это была моя первая статья для tylerneylon.com, которая так же доступна тут <a href="https://gist.github.com/tylerneylon/5853042">github gist</a>.
Удачи с Lua!
This was first written for tylerneylon.com, and is
also available as a <a href="https://gist.github.com/tylerneylon/5853042">github gist</a>. Have fun with Lua!