Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 50

UPGRADE YOUR KOTLIN CLASS GAME

Оглавление

You've already had a solid introduction to Kotlin, including building your first object. So we're going to get right to work. There's that nagging issue with a person's last name showing incorrectly, but before fixing that, you're ready to up how you build classes from a pre-compile point of view.

So far, you've used a single file to contain both your class ( Person) and your main function. That's not scalable, though. Before long, you're going to have lots of classes—and that's classes, not instances of classes.

NOTE As a quick refresher, a class is a definition. You have a Person class, and you might also have a Car class or a House class. An instance of a class is a specific piece of memory that holds a representation of an object. In the last chapter, when you created a brian or a rose variable, you assigned those instances of the Person class.

It's pretty easy to get lots of instances of a class, and then to also have lots of different classes defined as well. In those cases, things get messy if you're just using a single file.

If you throw all your classes into one file, that file is going to be a mess. It's better to separate each of your classes into their own files: one file per class (in general), and then if you have a main function, give that its own file, too.

Programming Kotlin Applications

Подняться наверх