当前位置:网站首页> Kotlin函数使用示例教程
Kotlin函数使用示例教程
2022-06-27 12:53:00 【1024问】
我们先看看简单的函数
我们写一个求和函数
函数头
函数体
调用函数非常简单
反编译Java源代码步骤
接下来我们来看看匿名函数
这是接口的声明
我们先看看简单的函数// 前面的文章我们了解到它是入口函数fun main() {}// 对应的Java 语言,熟悉的Java 的朋友会发现,哈哈,这就是一个Java静态方法// 我们通过两种语言的对比,能够对函数有一个更好的理解public static final void main() {}// 如果没有 Java 基础,不适合看本篇文章,本篇文章针对有Java基础并想深入学习Kotlin 的小伙伴我们写一个求和函数fun sum(a: Int, b: Int): Int { return a + b}sum函数有几部分构成呢?两部分,函数头和函数体。
函数头fun 函数定义关键字
sum 函数名
(a: Int, b: Int) 函数参数
: Int 函数返回类型
函数体左大括号开始,右大括号结束,中间是语句
// 我们只写了一行执行语句 return a + b调用函数非常简单fun main() { val sum = sum(10, 20) println(sum)}调用sum函数,传递两个Int 类型参数。返回值也是Int类型。返回结果 30
我们来看看反编译后的 Java源代码
public static final int sum(int a, int b) { return a + b; }反编译Java源代码步骤打开Intellij IDEA,按下Shift 键两次,输入show kotlin



定义函数时没有写名字,称为匿名函数。比如下面这个函数就是匿名函数
val f0: () -> String = { val currentYear = 2021 "Welcome to $currentYear"}这个函数来一起猜猜有几部分组成!!!哈哈,你答对的概率大概三分之一。
函数类型 () -> String函数体又称为lambda表达式 { val currentYear = 2021 "Welcome to $currentYear" } 对,它还称为匿名函数定义。
最后 val f0 是函数定义的引用。
我们反编译后,查看对应的Java 代码
public final class AnonymousFunKt { @NotNull private static final Function0 f0; @NotNull public static final Function0 getF0() { return f0; } public static final void main() { Object var0 = f0.invoke(); boolean var1 = false; System.out.println(var0); } // $FF: synthetic method public static void main(String[] var0) { main(); } static { f0 = (Function0)null.INSTANCE; }}我们注意到函数的实例话没有写,有可能是Kotlin不想让我们知道函数的实例化。
Function0 代表没有参数,f0调用invoke方法,返回结果后,打印出结果。
public interface Function0<out R> : Function<R> { /** Invokes the function. */ public operator fun invoke(): R}以上就是Kotlin函数使用示例教程的详细内容,更多关于Kotlin函数教程的资料请关注软件开发网其它相关文章!
边栏推荐
- Deploy redis sentinel mode using bitnamiredis Sentinel
- What else can PLM do?
- Principle of printf indefinite length parameter
- OpenHGNN发布0.3版本
- Vs debugging skills
- Deeply convinced plan X - system foundation summary
- Two TCP flow control problems
- GCC compiling dynamic and static libraries
- How to choose LAN instant messaging software
- Failed to execute NPM instruction, prompting ssh: Permission denied
猜你喜欢

TCP 流控问题两则

AI for Science: scientific research paradigm, open source platform and industrial form
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (III)

ThreadLocal 源码全详解(ThreadLocalMap)

让学指针变得更简单(一)

ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training

Cloud native (30) | kubernetes' app store Helm

Industry insight - how should brand e-commerce reshape growth under the new retail format?

带你认识图数据库性能和场景测试利器LDBC SNB

Neo4j: basic introduction (I) installation and use
随机推荐
基于SSM实现招聘网站
Postman如何设置成中文?(汉化)
[medical segmentation] unet3+
创建Deployment后,无法创建Pod问题处理
如何下载带有超链接的图片
与生活握手言和
Tiktok practice ~ public / private short video interchange
阿胖的操作记录
What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!
Realization of hospital medical record management system based on JSP
Review summary of database
新华三的千亿企业梦,还得靠吃ICT老本来实现?
The world's fastest download tool XDM
What else can PLM do?
基于JSP实现医院病历管理系统
#yyds干货盘点# 解决剑指offer:剪绳子(进阶版)
Local visualization tool connects to redis of Alibaba cloud CentOS server
PLM还能怎么用?
ZABBIX supports nail alarm
这是什么空调?