Kotlin for Android Developers
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 14 Visibility Modifiers
- 14.1 Modifiers private
Inline functions
Inline functions are a bit different from regular functions. An inline function will be substituted by its code during compilation, instead of really calling to a function. It will reduce memory allocations and runtime overhead in some situations. For instance, if we have a function as an argument, a regular function will internally create an object that contains that function. On the other hand, inline functions will substitute the code of the function in the place where it is called, so it won’t require an internal object for that. Another example: we could create blocks of code that are only executed if the version is Lollipop or newer: 13 Lambdas 49 1 inline fun supportsLollipop(code: () -> Unit) { 2 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 3 code() 4 } 5 } It just checks the version and executes the code if it meets the requirements. Now we could do: 1 supportsLollipop { 2 window.setStatusBarColor(Color.BLACK) 3 } For instance, Anko is also based on this idea to implement the DSL for Android layouts. You can also check an example from Kotlin reference, where a DSL to write HTML²⁰ from code is created. ²⁰ http://kotlinlang.org/docs/reference/type-safe-builders.html 14 Visibility Modifiers Modifiers are a bit different in Kotlin from how we use them in Java. The default modifier in this language is public , which savse a lot of time and characters. But here it is the long explanation. How do the different visibility modifiers work in Kotlin? 14.1 Modifiers private The private modifier is the most restrictive we can use. It implies it will only be visible inside its own file. So if we declare a class as private , we won’t be able to use it outside the file where it was defined. On the other hand, if we use private inside a class, the access is restricted to that class. Even classes that extend it won’t be able to use it. So first level classes, objects, interfaces… (known as package members) declared as private are only visible inside the file where they are declared, while everything defined inside a class or interface will only be visible by that class or interface. protected This modifier only applies to members inside a class or an interface. A package member cannot be protected . Inside a member, it works the same way as in Java: it can be used by the member itself and the members that extend it (for instance, a class and its subclasses). internal An internal member is visible inside the whole module if it’s a package member. If it’s a member inside another scope, it depends on the visibility of the scope. For instance, if we write a private class, the access to an internal function will be limited to the visibility of the class. We can use internal classes from any other class in the same module, but not from another module. Download 1.04 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling