Programming Kotlin Applications. Бретт Мак-Лахлин

Чтение книги онлайн.

Читать онлайн книгу Programming Kotlin Applications - Бретт Мак-Лахлин страница 12

Programming Kotlin Applications - Бретт Мак-Лахлин

Скачать книгу

release). Download the release and follow the instructions and you'll be good to go.

Snapshot of Good IDEs helping you quickly find and fix errors.

      NOTE These instructions are intentionally a bit sparse. If you're using the command line already, you probably don't need a lot of hand holding. For almost everyone else, though, using an IDE really is the best approach. As a bonus, you can also use IntelliJ as a proxy for the compiler, so you may just want to save the time it would take you to mess with the command line and put it into coding Kotlin!

      Command-Line Kotlin on Mac OS X

      The easiest path to getting Kotlin working on Mac OS X is to use one of the package managers popular on Macs: either Homebrew (brew.sh) or MacPorts (www.macports.org). Both of these make getting Kotlin up and running trivial.

      For MacPorts, just run the following command:

      brett $ sudo port install kotlin

      This requires elevated permissions, but after it runs, you'll be all set.

      For Homebrew, first do an update:

      brett $ brew update

      Next up, install Kotlin:

      Command-Line Kotlin on UNIX-Based Systems

      If you're not on Mac OS X but still have a Unix flavor of operating system, you can use SDKMAN! (sdkman.io) for installing Kotlin.

      NOTE To be accurate, Mac OS X is a Unix-based operating system, so you can use the SDKMAN! instructions for Macs instead of Homebrew or MacPorts.

      First, get SDKMAN!:

      brett $ curl -s https://get.sdkman.io | bash

      When you're finished, you'll need to open a new terminal or shell window or source the modified file as indicated at the end of the installation process.

      Now, install Kotlin:

      brett $ sdk install kotlin

      Verify Your Command-Line Installation

      However you've chosen to install Kotlin, when you're finished, you should be able to validate your installation with this command:

      brett $ kotlinc

      WARNING At this point, you may get prompted to install a Java runtime. This should fire up your system to handle this, and you can accept the prompts without a lot of worry. Find the JDK or JRE for your system, download it, and run it. Then come back and try out kotlinc again.

      If you have your system appropriately configured with Java, you should get back something like this:

      brett $ kotlinc Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release. Welcome to Kotlin version 1.3.61 (JRE 13.0.2+8) Type :help for help, :quit for quit >>>

      This is the Kotlin REPL (Read-Eval-Print Loop), a tool for quickly evaluating Kotlin statements. We'll look at this in more detail later, but for now, exit the REPL by typing :quit .

      brett $ kotlin -version Kotlin version 1.3.61-release-180 (JRE 13.0.2+8)

      At this point, you're ready to roll!

      With a working Kotlin environment, it's time to go make that Person class from Listing 1.2 something less vacuous. As mentioned earlier, objects should model real-world objects. Specifically, if an object represents a “thing” in the world (or in formal circles you'll sometimes hear that objects are “nouns”), then it should have the properties or attributes of that thing, too.

      A person's most fundamental property is their name, specifically a first name and last name (or surname, if you like). These can be represented as properties of the object. Additionally, these are required properties; you really don't want to create a person without a first and last name.

      For required properties, it's best to require those properties when creating a new instance of an object. An instance is just a specific version of the object; so you might have multiple instances of the Person class, each one representing a different actual person.

      WARNING As you may already be figuring out, there's a lot of technical vocabulary associated with classes. Unfortunately, it will get worse before it gets better: instances and instantiation and constructors and more. Don't get too worried about catching everything right away; just keep going, and you'll find that the vocabulary becomes second nature faster than you think. In Chapter 3, you'll dive in deeper, and in fact, you'll keep revisiting and growing your class knowledge throughout the entire book.

      Pass In Values to an Object Using Its Constructor

      An object in Kotlin can have one or more constructors. A constructor does just what it sounds like: it constructs the object. More specifically, it's a special method that runs when an object is created. It can also take in property values—like that required first and last name.

      You put the constructor right after the class definition, like this:

      class Person constructor(firstName: String, lastName: String) {

      WARNING You'll sometimes hear properties or property values called parameters. That's not wrong; a parameter is usually something passed to something else; in this case, something (a first name and last name) passed to something else (a constructor). But once they're assigned to the object instance, they're no longer parameters. At that point, they are properties (or more accurately, property values) of the object. So it's just easier to call that a property value from the start.

      See? The terminology is confusing. Again, though, it will come with time. Just keep going.

      class

Скачать книгу