当前位置:网站首页> 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函数教程的资料请关注软件开发网其它相关文章!
边栏推荐
- The browser enters the URL address, and what happens to the page rendering
- 中证500股指期货怎么开户,国内正规的股指期货平台有哪些,在哪里开户最安全?
- What is low code for digital Nova? What is no code
- Good luck today
- Deeply convinced plan X - system foundation summary
- Nifi from introduction to practice (nanny level tutorial) - identity authentication
- scrapy
- 执行 npm 指令失败,提示ssh: ... Permission denied
- 外包2年的我终于上岸了!记录我的字节跳动3轮面试,希望帮助到大家!
- 微服务如何拆分
猜你喜欢

ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training
![[dynamic programming] - Knapsack Problem](/img/27/c48284f15e3f80305d7ce7c02d4378.png)
[dynamic programming] - Knapsack Problem

浅谈软件研发的复杂性与效能提升之道

Realization of hospital medical record management system based on JSP

Deeply convinced plan X - system foundation summary
![[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)

Record number of visits yesterday

Bluetooth health management device based on stm32

深信服X计划-系统基础总结

What kind of air conditioner is this?
随机推荐
Steps for win10 to completely and permanently turn off automatic updates
Review summary of database
Centos7命令行安装Oracle11g
Ali an interview question: use two threads to output letters and numbers alternately
Pyqt, pyside slot functions are executed twice
Threejs' ambient light + point light + parallel light + spherical light and Hepler understanding + shadow ()
每日刷題記錄 (六)
Yuweng information, a well-known information security manufacturer, joined the dragon lizard community to build an open source ecosystem
JSON. Stringify usage
每日刷题记录 (六)
Intranet learning notes (8)
The browser enters the URL address, and what happens to the page rendering
Database Series: MySQL index optimization and performance improvement summary (comprehensive version)
How to open an account for CSI 500 stock index futures, what are the regular domestic stock index futures platforms, and where is the safest place to open an account?
How to modify a node_ Files in modules
Script defer async mode
AGCO AI frontier promotion (6.27)
Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
【周赛复盘】LeetCode第81场双周赛
如何下载带有超链接的图片