0
点赞
收藏
分享

微信扫一扫

Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2...

木匠0819 2022-10-17 阅读 105


5.1 扩展函数

In this codelab you are introduced to a number of different useful features in Kotlin, including pairs, collections, and extension functions.





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_02





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_03





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_04




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_05




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_06





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_07




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_08


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.




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_09




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_10




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_11




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_12


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.

​​Kotlin coding conventions​​

​​Kotlin idioms​​

​​Pair​​

​​Triple​​

​​List​​

​​MutableList​​

​​HashMap​​

​​Companion objects​​

​​Extensions​​

​​Nullable receiver​​

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.




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_13




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_14





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_15




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_16




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_17




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_18


如何声明和使用具有泛型约束的泛型类?




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_19




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_20




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_21




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_22




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_23




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_24




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_25


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.




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_26


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.

​​Kotlin coding conventions​​

​​Kotlin idioms​​

​​Generics​​

​​Generic constraints​​

​​Star-projections​​

​​In and out types​​

​​Reified parameters​​

​​Type erasure​​

​​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​​.




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_27




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_28




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_29




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_30


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.





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_31




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_32




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_33




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_34




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_35





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_36




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_37




Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._android_38


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.





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_39





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_40





Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._sed_41


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.

​​Kotlin coding conventions​​

​​Kotlin idioms​​

​​Annotations​​

​​Reflection​​

​​Labeled breaks​​

​​Higher-order functions and lambdas​​

​​Inline functions​​

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 开发者社区



Google 重申对 Kotlin 编程语言 Android 的承诺:谷歌推出 Kotlin 免费在线课程 Kotlin Bootcamp Course 2..._ide_42


国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。

越是喧嚣的世界,越需要宁静的思考。

举报

相关推荐

Cocos2d-x 2,android开发语言kotlin

0 条评论