Kotlin编码规范

本文详细介绍了Kotlin编程语言的当前编码格式,包括命名格式、Lambda表达式、Unit用法等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资料原文:http://www.kotlinlang.org/docs/reference/coding-conventions.html

本文包含了KotLin语言的当前编码格式。

命名格式

默认情况下采用Java编码规范:

  1. 采用驼峰式命名
  2. 类型以大写字母开头
  3. 方法和属性以小写字母开始
  4. 使用4个空格缩进
  5. 公有方法应该有类似Kotlin的文本文档

冒号

分开类型和父类的冒号之前有一个空格,在类型和实例分割的冒号钱没有空格

interface Foo<out T : Any> : Bar {
    fun foo(a: Int): T
}

Lambdas表达式

In lambda expressions, spaces should be used around the curly braces, as well as around the arrow which separates the parameters from the body. Whenever possible, a lambda should be passed outside of parenthes

list.filter { it > 10 }.map { element -> element * 2 }

lambdas which are short and not nested, it’s recommended to use the it convention instead of declaring the parameter explicitly. In nested lambdas with parameters, parameters should be always declared explicitly.

Unit

如果一个函数的返回类型为Unit,这个返回类型可以被忽略,该关键字的作用是函数不需要返回:

fun foo() { // ": Unit" is omitted here

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值