Kotlin学习(一)初步印象

简介

Kotlin is a modern but already mature programming language aimed to make developers happier. It’s concise, safe, interoperable with Java and other languages, and provides many ways to reuse code between multiple platforms for productive programming.

  • 官网: https://kotlinlang.org/
  • 最新语言版本: v1.6.10 (当前日期2021-12-18)

Learning materials overview

You can use the following materials and resources for learning Kotlin:

  • Basic syntax – get a quick overview of the Kotlin syntax.
  • Idioms – learn how to write idiomatic Kotlin code for popular cases.
  • Java to Kotlin migration guide: Strings – learn how to perform typical tasks with strings in Java and Kotlin.
  • Kotlin Koans – complete exercises to learn the Kotlin syntax. Each exercise is created as a failing unit test and your job is to make it pass.
  • Kotlin by example – review a set of small and simple annotated examples for the Kotlin syntax.
  • Kotlin Basics track – learn all the Kotlin essentials while creating working applications step by step on JetBrains Academy.
  • Advent of Code puzzles – learn idiomatic Kotlin and practice your language skills by completing short and fun tasks.
  • Kotlin books – find books we’ve reviewed and recommend for learning Kotlin.
  • Kotlin hands-on tutorials – complete long-form tutorials to fully grasp a technology. These tutorials guide you through a self-contained project related to a specific topic.
  • Kotlin for Java Developers – course for developers with experience in Java. It shows the similarities between the two languages and focuses on what’s going to be different.
  • Kotlin documentation in the PDF format – read the whole documentation offline.

Kotlin Docs Home

Keywords and operators

Grammar

Kotlin language specification

Kotlin Hands-On Labs

示例学习与初步印象

代码示例

package com.example.demokotlin

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication
class DemoKotlinApplication

fun main(args : Array<String>) {
runApplication<DemoKotlinApplication>(*args)
}

@RestController
class MessageResource {
@GetMapping
fun index(): List<Message> = listOf(
Message("1", "Hello!"),
Message("2", "Bonjour!"),
Message("3", "Privet!"),
)
}

data class Message(val id: String?, val text: String)
  1. 代码文件保存为.kt后缀的文件
  2. 与java相同有包路径定义,导入相关类等
  3. 类定义与java相同使用class关键字
  4. 方法定义使用fun关键字,参数则是名称在前类型在后,使用冒号隔开
  5. 不需要导入即可使用标准包中类的方法
  6. 可以使用data关键字来修饰类定义,表明是数据类,该类不需要实体,即没有java的getter和setter等一系列方法




listOf方法

这个方法定义在Kotlin标准库 kotlin-stdlib的Collections.kt中

/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
@file:OptIn(kotlin.experimental.ExperimentalTypeInference::class)

package kotlin.collections

...

/**
 * Returns a new read-only list of given elements. The returned list is serializable (JVM).
 * @sample samples.collections.Collections.Lists.readOnlyList
 */
public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()

上面是IDE中按住Ctrl点击listOf方法后进入Collections.kt的部分内容,开头@file这部分定义还不太清楚,另外文件里面的方法有访问修饰符。

Comments are closed