What is a module?
According to Jetbrains definition, a module is a discrete unit of functionality which you
can compile, run, test and debug independently. It basically refers to the Android Studio
modules we can create to divide our project into different blocks. In Eclipse, these modules
would refer to the projects inside a workspace.
50
14 Visibility Modifiers
51
public
As you may guess, this is the less restrictive modifier. It’s the default modifier, and the member
declared as public is visible anywhere, obviously restricted by its scope. A public member defined
in a private class won’t be visible outside the scope where the class is visible.
14.2 Constructors
By default, all constructors are
public
, which means they can be used from any scope where their
class is visible. But we can make a constructor private using this specific syntax:
1
class C private constructor(a: Int) { ... }
14.3 Revising our code
We’ve already been making use of the
public
default refactor, but there are many other details we
could change. For instance, in
RequestForecastCommand
, the property we create from the
zipCode
constructor parameter could be private.
1
class RequestForecastCommand(private val zipCode: String)
The thing is that as we are making use of immutable properties, the
zipCode
value can only be
requested, but not modified. So it is not a big deal to leave it as
public
, and the code looks cleaner.
If, when writing a class, you feel that something shouldn’t be visible by any means, feel free to make
it
private
.
Besides, in Kotlin we don’t need to specify the return type of a function if it can be computed by the
compiler. An example of how we can get rid of the returning types:
1
data class ForecastList(...) {
2
fun get(position: Int) = dailyForecast[position]
3
fun size() = dailyForecast.size()
4
}
The typical situations where we can get rid of the return type are when we assign the value to a
function or a property using
equals
(=) instead of writing a code block.
The rest of the modifications are quite straightforward, so you can check them in the repository.
Do'stlaringiz bilan baham: |