Programming Kotlin Applications. Бретт Мак-Лахлин
Читать онлайн книгу.This chapter breaks down your options, covering if and else, when, for, while, and do. Along the way, you'll focus on controlling the flow of an application or set of applications all while getting a handle on the semantics and mechanics of these structures.
Chapter 8: Data Classes This chapter introduces data classes, another very cool Kotlin concept. While not specific to only Kotlin, you'll find that data classes offer you a quick and flexible option for representing data more efficiently than older languages. You'll also really push data classes, going beyond a simple data object and getting into constructors, overriding properties, and both subclassing with and extending from data classes.
Chapter 9: Enums and Sealed, More Specialty Classes This chapter introduces enums, a far superior approach to String constants. You'll learn why using Strings for constant values is a really bad idea, and how enums give you greater flexibility and type safety, as well as making your code easier to write. From enums, you'll move into sealed classes, a particularly cool feature of Kotlin that lets you turbo-charge the concept of enums even further. You'll also dig into companion objects and factories, all of which contribute to a robust type-safe approach to programming where previously only String types were used.
Chapter 10: Functions and Functions and Functions It may seem odd to have a chapter this late in the book that purports to focus on functions. However, as with most fundamentals in any discipline, you'll have to revisit the basics over and over again, shoring up weaknesses and adding nuance. This chapter does just that with functions. You'll dig more deeply into just how arguments really work, and how many options Kotlin provides to you in working with data going into and out of your functions.
Chapter 11: Speaking Idiomatic Kotlin Kotlin, like all programming languages, has certain patterns of usage that seasoned programmers revert to time and time again. This chapter discusses these and some of the idioms of Kotlin. You'll get a jump start on writing Kotlin that looks like Kotlin is “supposed to” all while understanding how you have a tremendous amount of flexibility in choosing how to make your Kotlin programs feel like “you.”
Chapter 12: Inheritance, One More Time, with Feeling Yes, it really is another chapter on inheritance! This chapter takes what you've already learned about abstract classes and superclasses and adds interfaces and implementations into the mix. You'll also learn about the delegation pattern, a common Kotlin pattern that helps you take inheritance even further with greater flexibility than inheritance alone provides.
Chapter 13: Kotlin: The Next Step No book can teach you everything you need to know, and this book is certainly no exception. There are some well-established places to look for next steps in your Kotlin programming journey, though, and this chapter gives you a number of jumping-off points to continue learning about specific areas of Kotlin.
Reader Support for This BookCompanion Download Files
As you work through the examples in this book, the project files you need are available for download from www.wiley.com/go/programmingkotlinapplications
.
How to Contact the Publisher
If you believe you've found a mistake in this book, please bring it to our attention. At John Wiley & Sons, we understand how important it is to provide our customers with accurate content, but even with our best efforts an error may occur.
In order to submit your possible errata, please email it to our Customer Service Team at [email protected]
with the subject line “Possible Book Errata Submission.”
How to Contact the Author
We appreciate your input and questions about this book! Email me at [email protected]
, or DM me on Twitter at @bdmclaughlin
.
1 Objects All the Way Down
WHAT'S IN THIS CHAPTER?
A look at Kotlin syntax
A brief history of Kotlin
How Kotlin is like Java—and how it isn't
Getting set up to code and run Kotlin
Your first Kotlin program
Why objects are cool (and why that matters)
KOTLIN: A NEW PROGRAMMING LANGUAGE
Kotlin, when you boil it all down, is just another programming language. If you're programming and writing code already, you'll pick up Kotlin quickly, because it has a lot in common with what you're already doing. That's even more the case if you're programming in an object-oriented language, and if you're coding in Java, well, Kotlin is going to feel very familiar, although different in some very nice ways.
If you're new to Kotlin, though, it's a great first language. It's very clear, it doesn't have lots of odd idioms (like, for example, Ruby or, god help us all, LISP), and it's well organized. You'll pick it up fast and find yourself comfortable quite quickly.
In fact, Kotlin is so straightforward that we're going to put aside a lot of explanation and history for now, and instead jump right into looking at some basic Kotlin code (check out Listing 1.1).
LISTING 1.1: A simple Kotlin program using classes and lists
data class User(val firstName: String, val lastName: String) fun main() { val brian = User("Brian", "Truesby") val rose = User("Rose", "Bushnell") val attendees: MutableList<User> = mutableListOf(brian, rose) attendees.forEach { user -> println("$user is attending!") } }
Take a minute or two to read through this code. Even if you've never looked at a line of Kotlin before, you can probably get a pretty good idea of what's going on. First, it defines a User
class (and in fact, a special kind of class, a data class; more on that later). Then it defines a main
function, which is pretty standard fare. Next up are two variables (or vals
), each getting an instance of the User
class defined earlier. Then a list is created called attendees
and filled with the two users just created. Last up is a loop through the list, doing some printing for each.
If you ran this code, you'd get this rather unimpressive output:
User(firstName=Brian, lastName=Truesby) is attending! User(firstName=Rose, lastName=Bushnell) is attending!
Obviously, parts of this probably look odd to you, whether you're brand new to writing code or an experienced Java pro. On top of that, it's likely you have no idea how to actually compile or run this code. That's OK, too. We'll get to all of that.
NOTE It bears repeating: You really don't need to understand the code in Listing 1.1. This book assumes you've programmed at least a little bit—and it's true that you'll likely understand Kotlin a bit faster if you have a Java background—but you will pick up everything you see in Listing 1.1 (and quite a bit more) just by continuing to read and working through the code samples. Just keep going, and you'll be programming in Kotlin in no time.
For now, though, here's the point: Kotlin is really approachable, clean to read, and actually a pretty fun