hym1224 / kotlin

0 stars 0 forks source link

kotlin study #1

Open hym1224 opened 6 years ago

hym1224 commented 6 years ago

1:声明属性并在主构造函数中初始化,在 Kotlin 中有更简单的语法:

class Person(val firstName: String, val lastName: String, var age: Int) {
}

2: apply vs with apply函数是这样的,调用某对象的apply函数,在函数范围内,可以任意调用该对象的任意方法,并返回该对象 when to use, which to use Usually you use apply when you need to do something with an object and return it. And when you need to perform some operations on an object and return some other object you can use with.

3:使用命名参数我们可以让代码可读性更强

reformat(str,
    normalizeCase = true,
    uppercaseFirstLetter = true,
    divideByCamelHumps = false,
    wordSeparator = '_'
  )

4:kotlin调用java代码 ( getClass() ) To retrieve the Java class of an object, use the java extension property on a class reference:

val fooClass = foo::class.java

The code above uses a bound class reference, which is supported since Kotlin 1.1. You can also use the javaClass extension property:

val fooClass = foo.javaClass

5:!! 操作符 第三个选择是 NPE-lovers。我们可以用 b!! ,这会返回一个非空的 b 或者抛出一个 b 为空的 NPE

val l = b !!.length()

6:inline keyword from https://stackoverflow.com/questions/44471284/when-to-use-an-inline-function-in-kotlin

fun nonInlined(block: () -> Unit) {
    println("before")
    block()
    println("after")
}

public void nonInlined(Function block) {
    System.out.println("before");
    block.invoke();
    System.out.println("after");
}

nonInlined {
    println("do something here")
}

nonInlined(new Function() {
    @Override
    public void invoke() {
        System.out.println("do something here");
    }
});

// On the other hand, if you use the inline keyword:
inline fun inlined(block: () -> Unit) {
    println("before")
    block()
    println("after")
}
// When you call it like this:
inlined {
    println("do something here")
}

System.out.println("before");
System.out.println("do something here");
System.out.println("after");

7:run run函数和apply函数很像,只不过run函数是使用最后一行的返回,apply返回当前自己的对象。 from https://www.jianshu.com/p/5c4a954d2b2c

fun testRun() {
    // fun <T, R> T.run(f: T.() -> R): R = f()
    "testRun".run {
        println("this = " + this)
    }.let { println(it) }
}
// 运行结果
// this = testRun
// kotlin.Unit

// apply
fun testApply() {
    // fun <T> T.apply(f: T.() -> Unit): T { f(); return this }
    ArrayList<String>().apply {
        add("testApply")
        add("testApply")
        add("testApply")
        println("this = " + this)
    }.let { println(it) }
}

// 运行结果
// this = [testApply, testApply, testApply]
// [testApply, testApply, testApply]

8:Reified Types from https://antonioleiva.com/reified-types-kotlin/ https://blog.simon-wirtz.de/kotlin-reified-types/

9:lateinit & lazy 该修饰符只能用于在类体中的属性(不是在主构造函数中声明的 var 属性,并且仅当该属性没有自定义 getter 或 setter 时),而自 Kotlin 1.2 起,也用于顶层属性与局部变量。该属性或变量必须为非空类型,并且不能是原生类型。

在初始化前访问一个 lateinit 属性会抛出一个特定异常,该异常明确标识该属性被访问及它没有初始化的事实。

public class MyTest {
    lateinit var subject: TestSubject

    @SetUp fun setup() {
        subject = TestSubject()
    }

    @Test fun test() {
        subject.method()  // 直接解引用
    }
}

lazy() 是接受一个 lambda 并返回一个 Lazy 实例的函数,返回的实例可以作为实现延迟属性的委托: 第一次调用 get() 会执行已传递给 lazy() 的 lambda 表达式并记录结果, 后续调用 get() 只是返回记录的结果。

val lazyValue: String by lazy {
    println("computed!")
    "Hello"
}

fun main(args: Array<String>) {
    println(lazyValue)
    println(lazyValue)
}

from https://mindorks.com/blog/learn-kotlin-lateinit-vs-lazy

10:object 对象表达式,对象声明,单例模式

class Utils private constructor() {

    companion object {

        fun getScore(value: Int): Int {
            return 2 * value
        }

    }
}

// another way

object Utils {

    fun getScore(value: Int): Int {
        return 2 * value
    }

}

11:Extension Functions

fun Int.triple(): Int {
  return this * 3
}

var result = 3.triple()

12:Data Class

hym1224 commented 6 years ago

学习 Kotlin 的 20 个实用资源 https://zhuanlan.zhihu.com/p/27300530

hym1224 commented 6 years ago

kotlin in action https://panxl6.gitbooks.io/kotlin-in-action-in-chinese/

hym1224 commented 6 years ago

常量探究 http://droidyue.com/blog/2017/11/05/dive-into-kotlin-constants/

hym1224 commented 6 years ago

备用字段

  1. Kotlin中的属性(properties)和Java中的字段(field)概念上有极大的区别,相对于字段是更高层次的概念。
  2. Kotlin中有两种属性(properties):一种有后备属性(backing field),一种没有。

总结下来,对其理解就是 具备后备字段的属性。其实就是类中自己的属性。可读可写。 而不具备后备字段的属性,其实是一个代理?可读不可写。

hym1224 commented 6 years ago

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

https://stackoverflow.com/questions/35629159/kotlins-iterable-and-sequence-look-exactly-same-why-are-two-types-required

hym1224 commented 6 years ago

Where Should I Keep My Constants in Kotlin?

https://blog.egorand.me/where-do-i-put-my-constants-in-kotlin/

hym1224 commented 6 years ago

Java 8 Stream API Analogies in Kotlin

http://www.baeldung.com/java-8-stream-vs-kotlin

hym1224 commented 6 years ago

Exploring Kotlin’s hidden costs

hym1224 commented 6 years ago

Kotlin スコープ関数 用途まとめ https://qiita.com/ngsw_taro/items/d29e3080d9fc8a38691e