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

5.1 扩展函数

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

1233356-4f7ab4cf5d696834.png
1233356-2dc8110b3ff47afd.png


1233356-a23a72753f350517.png

1233356-c0402396951ff7b0.png
1233356-1a4b0a28d9418d30.png
1233356-2d01774d06728818.png

1233356-6899418eff0ed081.png
1233356-5d3247726cd7c25e.png

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.

1233356-f060d67956a57c40.png
1233356-fdcda215d92463b7.png
1233356-f1b5f0c2b0960b64.png
1233356-bf302bbdd77d9122.png

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.

1233356-7370b0830df7807b.png
1233356-2f3e23e6afb007d9.png

1233356-c57f8979f1d792d5.png
1233356-25050c22cf6f8efd.png
1233356-fa07f58ee4ea5c3a.png
1233356-01d49ba830099527.png

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


1233356-86b483170c9f82a8.png
1233356-36393e99938c34e6.png
1233356-357c08b3cc259d94.png
1233356-addac09cf8f866a0.png
1233356-1f4e98e0610f0854.png
1233356-533096dcd8c7e429.png
1233356-fb99c101c1a0af37.png

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.


1233356-2292eb1a3aae5ed9.png

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.

1233356-2869d0f7d2fd068b.png
1233356-078de189a26f9c2d.png
1233356-c5a5a9514eadb450.png
1233356-6627870365efe4c4.png
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.


1233356-e56e659a9271dc67.png
1233356-68d602d23ff3dfbc.png
1233356-6fb69ef300a47c41.png
1233356-c9ccdfa52d1138e8.png
1233356-a3108fac90299529.png


1233356-ea6f6dd60649937e.png
1233356-1d85030b76090812.png
1233356-fb751fd2650ac3e9.png
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.


1233356-4f14ef5c9f8c4b6f.png


1233356-6871d1c8d9578c55.png



1233356-d599ac6971f1181b.png

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


1233356-4cc10b922a41aa80

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

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

发布了1575 篇原创文章 · 获赞 648 · 访问量 63万+

猜你喜欢

转载自blog.csdn.net/universsky2015/article/details/104588505