5.1 扩展函数
In this codelab you are introduced to a number of different useful features in Kotlin, including pairs, collections, and extension functions.
The basic difference between companion objects and regular objects is:
Companion objects are initialized from the static constructor of the containing class, that is, they are created when the object is created.
Regular objects are initialized lazily on the first access to that object; that is, when they are first used.
There is more, but all that you need to know for now is to wrap constants in classes in a companion object.
7. Learn more
Kotlin documentation
If you want more information on any topic in this course, or if you get stuck, https://kotlinlang.org is your best starting point.
Pair
Triple
List
Kotlin tutorials
The https://try.kotlinlang.org website includes rich tutorials called Kotlin Koans, a web-based interpreter, and a complete set of reference documentation with examples.
Udacity course
To view the Udacity course on this topic, see Kotlin Bootcamp for Programmers.
IntelliJ IDEA
Documentation for the IntelliJ IDEA can be found on the JetBrains website.
5.2 泛型
In this codelab you are introduced to generic classes, functions, and methods, and how they work in Kotlin.
如何声明和使用具有泛型约束的泛型类?
5. Concept: Reified types and type erasure
In the earlier example, you had to mark the generic type as reified and make the function inline, because Kotlin needs to know about them at runtime, not just compile time.
All generic types are only used at compile time by Kotlin. This lets the compiler make sure that you're doing everything safely. By runtime all the generic types are erased, hence the earlier error message about checking an erased type.
It turns out the compiler can create correct code without keeping the generic types until runtime. But it does mean that sometimes you do something, like is checks on generic types, that the compiler can't support. That's why Kotlin added reified, or real, types.
You can read more about reified types and type erasure in the Kotlin documentation.
7. Learn more
Kotlin documentation
If you want more information on any topic in this course, or if you get stuck, https://kotlinlang.org is your best starting point.
check() function
Kotlin tutorials
The https://try.kotlinlang.org website includes rich tutorials called Kotlin Koans, a web-based interpreter, and a complete set of reference documentation with examples.
Udacity course
To view the Udacity course on this topic, see Kotlin Bootcamp for Programmers.
IntelliJ IDEA
Documentation for the IntelliJ IDEA can be found on the JetBrains website.
第6课 Functional manipulation
In this codelab you learn about annotations and labeled breaks. You review lambdas and higher- order functions, which are key parts of Kotlin. You also learn more about inlining functions, and Single Abstract Method (SAM) interfaces. Finally, you learn more about the Kotlin Standard Library.
Annotations are really powerful for creating libraries that inspect things both at runtime and sometimes at compile time. However, typical application code just uses annotations provided by frameworks.
It is worth noting that inlining large functions does increase your code size, so it's best used for simple functions that are used many times like myWith(). The extension functions from the libraries you learned about earlier are marked inline, so you don't have to worry about extra objects being created.
9. Learn more
Kotlin documentation
If you want more information on any topic in this course, or if you get stuck, https://kotlinlang.org is your best starting point.
Higher-order functions and lambdas
Kotlin tutorials
The https://try.kotlinlang.org website includes rich tutorials called Kotlin Koans, a web-based interpreter, and a complete set of reference documentation with examples.
Udacity course
To view the Udacity course on this topic, see Kotlin Bootcamp for Programmers.
IntelliJ IDEA
Documentation for the IntelliJ IDEA can be found on the JetBrains website.
Kotlin Standard Library
The Kotlin Standard Library provides numerous useful functions. Before you write your own function or interface, always check the Standard Library to see if someone has saved you some work. Check back occasionally, because new functionality is added frequently.
Kotlin tutorials
The https://try.kotlinlang.org website includes rich tutorials called Kotlin Koans, a web-based interpreter, and a complete set of reference documentation with examples.
Udacity course
To view the Udacity course on this topic, see Kotlin Bootcamp for Programmers.
IntelliJ IDEA
Documentation for the IntelliJ IDEA can be found on the JetBrains website.
11. Next steps
Congratulations! You've completed the Kotlin Bootcamp for Programmers codelab.
For an overview of the course, including links to other codelabs, see "Kotlin Bootcamp for Programmers: Welcome to the course."
If you're a Java programmer, you may be interested in the Refactoring to Kotlin codelab. The automated Java to Kotlin conversion tools cover the basics, but you can create more concise, robust code with a little extra work.
If you're interested in developing apps for Android, take a look at Android Kotlin Fundamentals.
Kotlin 开发者社区
国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。
越是喧嚣的世界,越需要宁静的思考。
标签:...,functions,Google,Kotlin,IDEA,course,Udacity,more From: https://blog.51cto.com/u_15236724/5763596