<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Lua 數據類型 Lua是動態類型語言,變量不要類型定義,只需要為變量賦值。 值可以存儲在變量中,作為參數傳遞或結果返回。 Lua中有8個基本類型分別為:nil、boolean、number、string、userdata、function、thread和table。 | 數據類型 | 描述 | | --- | --- | | nil | 這個最簡單,只有值nil屬于該類,表示一個無效值(在條件表達式中相當于false)。 | | boolean | 包含兩個值:false和true。 | | number | 表示雙精度類型的實浮點數 | | string | 字符串由一對雙引號或單引號來表示 | | function | 由 C 或 Lua 編寫的函數 | | userdata | 表示任意存儲在變量中的C數據結構 | | thread | 表示執行的獨立線路,用于執行協同程序 | | table | Lua 中的表(table)其實是一個"關聯數組"(associative arrays),數組的索引可以是數字或者是字符串。在 Lua 里,table 的創建是通過"構造表達式"來完成,最簡單構造表達式是{},用來創建一個空表。 | 我們可以使用type函數測試給定變量或者值的類型: ``` print(type("Hello world")) --> string print(type(10.4*3)) --> number print(type(print)) --> function print(type(type)) --> function print(type(true)) --> boolean print(type(nil)) --> nil print(type(type(X))) --> string ``` ## nil(空) nil 類型表示一種沒有任何有效值,它只有一個值 -- nil,例如打印一個沒有賦值的變量,便會輸出一個 nil 值: ``` > print(type(a)) nil > ``` 對于全局變量和 table,nil 還有一個"刪除"作用,給全局變量或者 table 表里的變量賦一個 nil 值,等同于把它們刪掉,執行下面代碼就知: ``` tab1 = { key1 = "val1", key2 = "val2", "val3" } for k, v in pairs(tab1) do print(k .. " - " .. v) end tab1.key1 = nil for k, v in pairs(tab1) do print(k .. " - " .. v) end ``` ## boolean(布爾) boolean 類型只有兩個可選值:true(真) 和 false(假),Lua 把 false 和 nil 看作是"假",其他的都為"真": ``` print(type(true)) print(type(false)) print(type(nil)) if type(false) or type(nil) then print("false and nil are false!") else print("other is true!") end ``` 以上代碼執行結果如下: ``` $ lua test.lua boolean boolean nil false and nil are false! ``` ## number(數字) Lua 默認只有一種 number 類型 -- double(雙精度)類型(默認類型可以修改 luaconf.h 里的定義),以下幾種寫法都被看作是 number 類型: ``` print(type(2)) print(type(2.2)) print(type(0.2)) print(type(2e+1)) print(type(0.2e-1)) print(type(7.8263692594256e-06)) ``` 以上代碼執行結果: ``` number number number number number number ``` ## string(字符串) 字符串由一對雙引號或單引號來表示。 ``` string1 = "this is string1" string2 = 'this is string2' ``` 也可以用 2 個方括號 "[[]]" 來表示"一塊"字符串。 ``` html = [[ <html> <head></head> <body> <a href="http://www.w3cschool.cc/">w3cschool菜鳥教程</a> </body> </html> ]] print(html) ``` 以下代碼執行結果為: ``` <html> <head></head> <body> <a href="http://www.w3cschool.cc/">w3cschool菜鳥教程</a> </body> </html> ``` 在對一個數字字符串上進行算術操作時,Lua 會嘗試將這個數字字符串轉成一個數字: ``` > print("2" + 6) 8.0 > print("2" + "6") 8.0 > print("2 + 6") 2 + 6 > print("-2e2" * "6") -1200.0 > print("error" + 1) stdin:1: attempt to perform arithmetic on a string value stack traceback: stdin:1: in main chunk [C]: in ? > ``` 以上代碼中"error" + 1執行報錯了,字符串連接使用的是 .. ,如: ``` > print("a" .. 'b') ab > print(157 .. 428) 157428 > ``` 使用 # 來計算字符串的長度,放在字符串前面,如下實例: ``` > len = "www.w3cschool.cc" > print(#len) 16 > print(#"www.w3cschool.cc") 16 > ``` ## table(表) 在 Lua 里,table 的創建是通過"構造表達式"來完成,最簡單構造表達式是{},用來創建一個空表。也可以在表里添加一些數據,直接初始化表: ``` -- 創建一個空的 table local tbl1 = {} -- 直接初始表 local tbl2 = {"apple", "pear", "orange", "grape"} ``` Lua 中的表(table)其實是一個"關聯數組"(associative arrays),數組的索引可以是數字或者是字符串。 ``` -- table_test.lua 腳本文件 a = {} a["key"] = "value" key = 10 a[key] = 22 a[key] = a[key] + 11 for k, v in pairs(a) do print(k .. " : " .. v) end ``` 腳本執行結果為: ``` $ lua table_test.lua key : value 10 : 33 ``` 不同于其他語言的數組把 0 作為數組的初始索引,在 Lua 里表的默認初始索引一般以 1 開始。 ``` -- table_test2.lua 腳本文件 local tbl = {"apple", "pear", "orange", "grape"} for key, val in pairs(tbl) do print("Key", key) end ``` 腳本執行結果為: ``` $ lua table_test2.lua Key 1 Key 2 Key 3 Key 4 ``` table 不會固定長度大小,有新數據添加時 table 長度會自動增長,沒初始的 table 都是 nil。 ``` -- table_test3.lua 腳本文件 a3 = {} for i = 1, 10 do a3[i] = i end a3["key"] = "val" print(a3["key"]) print(a3["none"]) ``` 腳本執行結果為: ``` $ lua table_test3.lua val nil ``` ## function(函數) 在 Lua 中,函數是被看作是"第一類值(First-Class Value)",函數可以存在變量里: ``` -- function_test.lua 腳本文件 function factorial1(n) if n == 0 then return 1 else return n * factorial1(n - 1) end end print(factorial1(5)) factorial2 = factorial1 print(factorial2(5)) ``` 腳本執行結果為: ``` $ lua function_test.lua 120 120 ``` function 可以以匿名函數(anonymous function)的方式通過參數傳遞: ``` -- function_test2.lua 腳本文件 function anonymous(tab, fun) for k, v in pairs(tab) do print(fun(k, v)) end end tab = { key1 = "val1", key2 = "val2" } anonymous(tab, function(key, val) return key .. " = " .. val end) ``` 腳本執行結果為: ``` $ lua function_test2.lua key1 = val1 key2 = val2 ``` ## thread(線程) 在 Lua 里,最主要的線程是協同程序(coroutine)。它跟線程(thread)差不多,擁有自己獨立的棧、局部變量和指令指針,可以跟其他協同程序共享全局變量和其他大部分東西。 線程跟協程的區別:線程可以同時多個運行,而協程任意時刻只能運行一個,并且處于運行狀態的協程只有被掛起(suspend)時才會暫停。 ## userdata(自定義類型) userdata 是一種用戶自定義數據,用于表示一種由應用程序或 C/C++ 語言庫所創建的類型,可以將任意 C/C++ 的任意數據類型的數據(通常是 struct 和 指針)存儲到 Lua 變量中調用。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看