Linux Format

Kotlin packages

-

Packages help you put Kotlin code in many source files, which will also help you put your classes into separate files. As a result, you’ll be able to copy the Kotlin code of an existing Kotlin class without having to copy unnecessar­y code or wondering whether you got all the required code or not!

The general structure of a Kotlin package takes the following form:

package eu.mtsoukalos.magazines class LXF(val year: Int, month: String) {

... }

A package begins with the package keyword followed by the name of the package. The source code of a Kotlin package is saved inside a directory that’s named after the package name.

In the previous code, you declared a package named magazines, which includes a class named LXF in it. You can now use the aforementi­oned package along with the LXF class defined in it in your programs as follows:

import eu.mtsoukalos.magazines.LXF var myLXF = LXF(2017, “May")

So, you need an import statement in order to use a class from an external package. After that, you can use the LXF class as if it were defined inside your Kotlin program. However, if you want to import all the classes of a Kotlin package, you should use an import eu.

mtsoukalos.magazines.* statement. Unfortunat­ely, talking more about Kotlin packages is beyond the scope of this tutorial.

Newspapers in English

Newspapers from Australia