博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lua中print
阅读量:7294 次
发布时间:2019-06-30

本文共 1189 字,大约阅读时间需要 3 分钟。

hot3.png

reference:

http://www.lua.org/manual/5.3/manual.html

 

print (···)

Receives any number of arguments and prints their values to stdout, using the  function to convert each argument to a string. print is not intended for formatted output, but only as a quick way to show a value, for instance for debugging. For complete control over the output, use  and.

 

tostring (v)

Receives a value of any type and converts it to a string in a human-readable format. (For complete control of how numbers are converted, use.)

If the metatable of v has a __tostring field, then tostring calls the corresponding value with v as argument, and uses the result of the call as its result.

示例:

a = {5, 6}print(a)--用c来做Metatablec = {}c.__tostring =function(set)    local s = "{"     local sep = ""    for _,e in pairs(set) do        s = s .. sep .. e        sep = ", "    end return s .. "}" endsetmetatable(a, c)print(a)

 

执行结果:

table: 0x1041150{5, 6}

 

注释:

1. 调用第一个打印print(a),表示a为一个table,以及对应的地址。即print 函数调用 tostring 来格式化的输出。

2.调用第二个打印print(a),结果为__tostring的返回值。即当格式化一个对象的时候,tostring 会首先检查对象是否存在一个带有__tostring 域的 metatable。如果存在则以对象作为参数调用对应的函数来完成格式化,返回的结果即为 tostring 的结果。

转载于:https://my.oschina.net/u/2326611/blog/840847

你可能感兴趣的文章
epoll_wait会被系统中断唤醒
查看>>
Java设计模式-代理模式
查看>>
Android--sharepreference总结
查看>>
在博客园已经一年多时间了,今天开通博客了!
查看>>
给定矩阵行数和矩阵列数,顺时针打印矩阵(从0开始)
查看>>
个人阅读作业week7
查看>>
Java数据类型(2)------自动封装拆箱
查看>>
java基本语法
查看>>
oracle多表关联多字段update
查看>>
欧拉函数
查看>>
AngularJS源码解析4:Parse解析器的详解
查看>>
HTTP错误 404.17 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理...
查看>>
busybox inetd tftpd
查看>>
busybox reboot 无效
查看>>
hdu6312 2018杭电多校第二场 1004 D Game 博弈
查看>>
制作jar文件
查看>>
Jquery 实现回车键触发功能
查看>>
netty-socketio使用namespace
查看>>
在div中设置文字与内部div垂直居中
查看>>
JS入门篇(1)
查看>>