Simple Robot v4.6.0 Help

上手 Kotlin!

尽管 Simple Robot 是一个努力保证 Java 友好的框架,但是无论如何,它首先是使用 Kotlin 开发的。 那么,直接使用 Kotlin 进行开发的体验终究会比使用 Java 要更好。

因此我们在此建议, 优先使用 Kotlin 来进行 Simple Robot 项目开发

得益于 Kotlin 的 Java 兼容性 ,你可以在 Kotlin 中使用几乎所有的 Java 库, 例如 Spring。

何谓 Kotlin

引用Kotlin 官网中的介绍:

引用Kotlin 中文站中的介绍:

引用灰蓝天际的博客中的介绍:

上手 Kotlin

官网、文档与资料

学习与实操

  • Koans: a series of exercises to get you familiar with the Kotlin syntax and some idioms.

    (Koans: 让你熟悉 Kotlin 语法和关键词的一系列练习)

  • Learn Kotlin by Example: An official set of small and simple annotated examples designed for those new to Kotlin.

    No prior knowledge of any programming language is required.

    (一套为 Kotlin 新手设计的官方小而简单的注释示例。无需任何编程语言知识。)

快速浏览

// 普通类 class SimpleClass // 抽象类 abstract class SimpleAbstractClass // 接口 interface SimpleInterface // 数据类(例如 POJO, DTO 等, 实现 toString, hashCode, equals) data class SimpleDataClass(val name: String) // 单例对象 object SimpleObject // 顶层函数 fun topFun() { }
// 抽象类 abstract class Person(val name: String) { abstract fun greet() } // 接口 interface FoodConsumer { fun eat() fun pay(amount: Int) = println("Delicious! Here's $amount bucks!") } // 普通 final 类 class RestaurantCustomer(name: String, val dish: String) : Person(name), FoodConsumer { fun order() = println("$dish, please!") override fun eat() = println("*Eats $dish*") override fun greet() = println("It's me, $name.") }
fun main() { val name = "stranger" // Declare your first variable println("Hi, $name!") // ...and use it! print("Current count:") for (i in 0..10) { // Loop over a range from 0 to 10 print(" $i") } }
import kotlinx.coroutines.* suspend fun main() { // 一个可以挂起与恢复的 '挂起' 函数 val start = System.currentTimeMillis() coroutineScope { // 创建一个作用域, 用来启动协程 for (i in 1..10) { launch { // 启动 10 个并行任务 delay(3000L - i * 300) // 延迟/等待 log(start, "Countdown: $i") } } } // 上面的作用域内所有异步任务结束后,执行 log(start, "Liftoff!") } fun log(start: Long, msg: String) { println("$msg " + "(on ${Thread.currentThread().name}) " + "after ${(System.currentTimeMillis() - start)/1000F}s") }
fun main() { // 调用函数,传入 Lambda run { // 大括号内即为入参的 Lambda println("Hello, World!") } } // 顶层函数,参数是一个 Lambda fun run(func: () -> Unit) { // 直接使用箭头表达式定义 Lambda 函数结构 // 执行这个 lambda 体 func() }
Last modified: 19 September 2024