EmmyLua 注解功能
前言
网上配置 EmmyLua 的方法很多,此处就不做赘述(因此前提是你已经安装配置完EmmyLua)
本文仅是对 EmmyLua插件 内 注解功能 用法的代码演示。因为网上大部分EmmyLua配置教程中都没对此部分进行讲解,而实际lua开发时EmmyLua的注解功能几乎必不可缺,故作此文
注解的目的
我们在编写C#脚本时,IDE的相关插件能提示各类方法或成员以及描述:

但Lua内,即便在安装完EmmyLua,不写注解的话,也就没有任何提示(灰色提示仅表示刚有写过该参数而已,完全不知道是成员变量或方法):

因此EmmyLua注解功能就是为了解决该问题:模拟实现OOP编程中代码提示

注解用法
类声明
基本格式:--@class MY_TYPE[:PARENT_TYPE] [@comment]
---@class Person 人
Person = {};
---@class Gamer : Person 玩家
Gamer = {};
变量类型
基本格式:---@type MY_TYPE[|OTHER_TYPE] [@comment]
PS:按上述官方用法,comment描述应当放在末尾,但我这试了下type不大行,可以放在顶部
---@type number 我的ID
myId = 1;
---玩家的表
---@type table<number, Gamer>
gamersTable = {};
---@type Person Person实例
personA;
---@type Gamer Gamer实例
gamerA;
变量的额外属性
即使该类未持有某属性,也可以通过添加注解,在提示内出现(PS:其实EmmyLua实现UnityAPI的提示也是基于此的)
基本格式:---@field [public|protected|private] field_name FIELD_TYPE[|OTHER_TYPE] [@comment]
---@class Person 人
---@field public Name string 名字
---@field private m_Age number 年龄
Person = {
Name = "",
};
函数
---取得台词
---@param isCN boolean 是否是中文
---@param id number 台词字典ID
---@return string 台词
function GetLines(isCN, id)
local str = ""; -- do something
return str;
end
备注
在IDEA下,对目标使用Alt+Enter快捷键(或点小灯泡),可较方便自动补全注解:

完整事例
现有Person基类,Gamer类继承自Person,在Main.lua内实现两个类的创建及使用(直接粘到本地跑就行):
Main.lua:
require("Person");
require("Gamer");
---@type Person
local pa = Person:Create("joker", 18);
pa:ShowInfo();
pa:ReName("Joker");
pa:ShowInfo()
---@type Gamer
local ga = Gamer:Create("fox", 19, nil, nil);
ga:ShowInfo();
ga:ReName("Fox");
ga:ReGamerInfo("123", "456");
ga:ShowInfo();
Person.lua:
---@class Person 人类型
---@field public Name string 名字
---@field private m_Age number 年龄
Person = {
Name = "",
m_Age = 0,
};
Person.__index = Person;
---Create
---@param name string
---@param age number
function Person:Create(name, age)
---@type Person
local t = {};
setmetatable(t, Person);
t:ReName(name);
t:ReAge(age);
return t;
end
---ReName
---@param newName string
---@public
function Person:ReName(newName)
self.Name = newName;
end
---ReAge
---@param newAge number
---@private
function Person:ReAge(newAge)
self.m_Age = newAge;
end
---ShowInfo
---@public
function Person:ShowInfo()
print("Name = " .. self.Name .. ", Age = " .. self.m_Age);
end
Gamer.lua:
require("Person")
---@class Gamer : Person 玩家
---@field private SW string SW码
---@field private SteamId string Steam链接
Gamer = {
SW = "",
SteamId = "",
};
Gamer.__index = Gamer;
setmetatable(Gamer, Person);
function Gamer:Create(name, age, sw, steamId)
---@type Gamer
local t = {};
t = Person:Create(name, age);
setmetatable(t, Gamer);
t:ReGamerInfo(sw, steamId);
return t;
end
---ReGamerInfo
---@param sw string
---@param steamId string
---@public
function Gamer:ReGamerInfo(sw, steamId)
self.SW = sw or "0";
self.SteamId = steamId or "0";
end
---ShowInfo
---@public
function Gamer:ShowInfo()
print("Name = " .. self.Name .. ", Age = " .. self.m_Age .. ", SW = " .. self.SW .. ", SteamId = " .. self.SteamId);
end
PS:可以用 在Lua中实现面向对象特性——模拟类、继承、多态 - 马三小伙儿 大佬这篇的代码练手
EmmyLua 注解功能的更多相关文章
- Struts2的注解功能
我们知道通常情况下,Struts2是通过struts.xml配置的.但是随着系统规模的加大我们需要配置的文件会比较大,虽然我们可以根据不同的系统功能将不同模块的配置文件单独书写,然后通过<inc ...
- springboot~Compiler时开启插件的注解功能
对于IJ这个IDE工具来说,我们会安装一些插件来帮助我们更好的进行开发,像lombok就是一款不错的插件,使用注解的方式在项目编译时帮助我们生成代码,像getter,setter,tostring等等 ...
- Highcharts图表的注解功能
Highcharts图表的注解功能 在图表中,往往须要对图表总体或者部分元素进行对应注解.帮助浏览者阅读图表.尽管标签组labels能够实现类似的功能.可是其功能相对简单.要实现复杂的注解功能,用户能 ...
- @Deprecated注解功能
@Deprecated注解功能 标记不建议使用的方法,但是仍然可以用 当方法有更好的方法替换时,但是此方法还有使用时可以使用该注解
- mybatis的注解功能
一.mybatis 简单注解 关键注解词 : @Insert : 插入sql , 和xml insert sql语法完全一样 @Select : 查询sql, 和xml select sql语法完全一 ...
- spring源码解析:元注解功能的实现
前言 众所周知,spring 从 2.5 版本以后开始支持使用注解代替繁琐的 xml 配置,到了 springboot 更是全面拥抱了注解式配置.平时在使用的时候,点开一些常见的等注解,会发现往往在一 ...
- spring 中的一些注解功能--不定更新
1@Qualifier注解? 下面的示例将会在Customer的person属性中自动装配Person的值. public class Customer { @Autowired private Pe ...
- vert.x框架-使用spring注解功能
1.前言 习惯了spring注解风格,方便好用,现在用vert.x框架,怎么使用spring注解呢? 2.maven安装依赖包 <!--spring注解依赖包--> <depende ...
- spring--启用注解功能
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.spr ...
随机推荐
- 设计模式学习-使用go实现观察者模式
观察者模式 定义 适用场景 优点 缺点 代码实现 不同场景的实现方式 观察模式和发布订阅模式 参考 观察者模式 定义 观察者模式(Observer Design Pattern)定义了一种一对多的依赖 ...
- JDBC操作多张表一
一.操作一对多情况开发步骤1创建对象 //代码部门的对象public class Department { private String id; private String name; privat ...
- [loj2339]通道
类似于[loj2553] 对第一棵树边分治,对第二棵树建立虚树,并根据直径合并的性质来处理第三棵树(另外在第三棵树中计算距离需要使用dfs序+ST表做到$o(1)$优化) 总复杂度为$o(n\log^ ...
- [atAGC045F]Division into Multiples
令$d=\gcd(a,b)$,可以发现$c|(ax+by)$等价于$lcm(c,d)|(ax+by)$,因此不妨令$c'=lcm(c,d)$,然后将$a$.$b$和$c$同时除以$d$ 接下来设$(a ...
- 4、使用SetOperations(无序)操作redis(Set集合)
文章来源:https://www.cnblogs.com/shiguotao-com/p/10560599.html 方法 c参数 s说明 Long add(K key, V... values); ...
- vue-if和show
<template> <div> <div v-if="flag">今晚要上课</div> <div v-else> 今 ...
- JavaScript中的多种进制与进制转换
进制介绍 JavaScript 中提供的进制表示方法有四种:十进制.二进制.十六进制.八进制. 对于数值字面量,主要使用不同的前缀来区分: 十进制(Decimal): 取值数字 0-9:不用前缀. 二 ...
- 洛谷 P5391 - [Cnoi2019]青染之心
洛谷题面传送门 介绍一种假做法,期望复杂度应该比较优秀,但可以卡掉( 首先这个问题显然严格强于只有添加元素的情况对吧,而只有添加元素的情况就是一个普通的背包,而只有插入操作的版本复杂度就已经达到了 \ ...
- shell 脚本在linux中的应用
shell脚本在linux中应用广泛,之前一直选用python写脚本来进行一些文件操作,但是最后发现shell脚本非常方便,所以特意来学习下皮毛,便于提高自己效率 定义变量 1 country=&qu ...
- selenium+chrome抓取数据,运行js
某些特殊的网站需要用selenium来抓取数据,比如用js加密的,破解难度大的 selenium支持linux和win,前提是必须安装python3,环境配置好 抓取代码: #!/usr/bin/en ...