Walkthrough - Kotlin: Packages and Imports
In this lesson, we explored how Kotlin organizes code using packages and how to bring that code into other files using various import strategies.
Changes Made
Implementation
- Created Utils.kt in a sub-package
learn_kotlin.otherto provide entities for importing. - Created Packages.kt in the main
learn_kotlinpackage which demonstrates:- Single Imports: Importing a specific function.
- Wildcard Imports: Importing all members of a package.
- Import Aliasing: Using the
askeyword to resolve name clashes (e.g.,MessagevsOtherMessage). - Default Imports: Utilizing classes like
kotlin.maththat are available without explicit headers.
- Updated App.java to execute the package and import demonstration.
Verification
- Build: Successfully executed
./gradlew :app:assemble. - Logic: Verified that the aliased import correctly refers to the class in the
otherpackage while the localMessageclass remains accessible.
See the Step-by-Step Explanation for technical details.
Comments
Post a Comment