Kotlin那些鲜为人知的秘密:1.基础入门

什么要学习Kotlin

首先,看这篇文章前,应该先明确一个问题:咱们为何要学习Kotlin?java

以下图所示:git

k1.png

而Kotlin是一门很是优秀的语言,兼容了N多种语言的优势,学习Kotlin有助于提高咱们多层编程开发的认识。github

从一个类开始

感谢开源项目https://github.com/githubwing/GankClient-Kotlin编程

以及https://github.com/huanglizhuo/kotlin-in-chinese的Kotlin翻译api

咱们从GankClient的某个类开始学习Kotlin安全

这是Kotlin的一个样例类(终于不用再像java同样写多余的分号了):并发

package com.wingsofts.gankclient.mvp.model

import com.wingsofts.gankclient.api.GankApi
import com.wingsofts.gankclient.bean.FuckGoods
import com.wingsofts.gankclient.bean.JsonResult
import com.wingsofts.gankclient.mvp.contract.RandomContract
import rx.Observable
import javax.inject.Inject

class RandomModel
@Inject constructor(private val api: GankApi) : RandomContract.Model {

    override fun getRandom(type: String): Observable<JsonResult<List<FuckGoods>>> {
        return api.getRandom(type)
    }

    var counter = 0
        set(value) {
            if (value >= 0)
                field = value
        }

    fun max(a: Int, b: Int) = if (a > b) a else b
}
复制代码

导包

与Java同样,Kotlin也含有默认导入的特性。Kotlin的每一个文件都默认导入了以下经常使用包,app

-- kotlin.*dom

-- kotlin.annotation.*ide

-- kotlin.collections.*

-- kotlin.comparisons.* (since 1.1)

-- kotlin.io.*

-- kotlin.ranges.*

-- kotlin.sequences.*

-- kotlin.text.*

而Kotlin的导包方式和Java也相似,但有一点,Kotlin不支持静态导入。

Kotlin为何不支持静态导入?

由于静态导入会增长代码的阅读成本。

函数声明

fun

Kotlin 中用关键字 fun 声明函数。

override fun getRandom(type: String): Observable<JsonResult<List<FuckGoods>>> {
        return api.getRandom(type)
    }
复制代码

Kotlin为何使用fun来声明函数? 为何不是function?或者def?

JetBrains团队说:

We use “fun” because we like it - and yes, we do know what the word means in English.

咱们使用“fun”由于咱们喜欢它,并且咱们知道这个词在英语中是啥意思!

“fun”在英语中是什么意思?

来自谷歌翻译的 fun 的翻译:动词 开玩笑,名词 玩笑。

参数

参数声明

Kotlin的参数使用Pascal符号定义,参数之间和java相似经过“ , ”分隔,参数须要指明类型。

fun test(count: Int, test2: Int) {
    }
复制代码

为何Kotlin像Pascal同样,参数声明类型要在参数名后面? count: Int

Rob Pike曾解释过这个问题:由于这样更加清晰易懂,固然,我以为这样存在很大的争议,不过也和工程师自身的习惯有关系。

默认参数

Kotlin参数能够设置默认值,当须要忽略该参数的时候可使用参数的默认值。Like this: off: Int = 0

fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size ) {
    ...
    }
复制代码

Kotlin参数为何支持默认值?

你据说太重载爆炸吗?🌰

2.png

空返回值

在Java中返回空关键字为void,在Kotlin中的空返回值为Unit,而且能够省略.Unit和void不一样,Unit是个真实的类而且是个单例。

// 不省略
fun printHello(name: String?): Unit {
    ...
}
// 省略
fun printHello(name: String?) {
    ...
}
复制代码

为何使用Unit?

“Unit” just stands for “something that has only one value”, it’s a traditional name, comes from functional languages. I agree that this name is not very intuitive, but we failed to invent a better name.

“Unit”表明“只有一个值”,它是一个来自函数式语言的传统的名称。我赞成这个名字不是很直观,但咱们没有想到一个更好的名字。

局部变量

Kotlin局部变量分为val和var

var 关键字声明可变属性,和Java变量声明相似;

val 关键字声明只读属性,和Java中final变量相似,在初始化时须要赋值,之后不能改变。更多的应该使用val关键字。

val a: Int = 1
    var x = 5
复制代码

为何使用val关键字?

val具备Immutable或readonly的特性,一旦建立,不可改变。没有竞争条件,没有并发问题,无需去同步,保证了线程安全,不须要担忧空安全问题。

为何是val和var?

由于val是value的缩写,而var是variable。

Using vals in your code makes you think about alternative, immutable, functional code. […] Removing vars leads to refactoring. The refactoring leads to new coding patterns. New coding patterns leads to a shift in your approach to programming. This shift in approach leads to transformative code that has fewer defects and is easier to maintain.

— Beginning Scala, David Pollack

相关文章
相关标签/搜索