M
M
Maxim Siomin2021-08-20 16:11:10
Kotlin
Maxim Siomin, 2021-08-20 16:11:10

When is it better to use variables with getters and when methods?

Here is my class:

/**
 * All classes that has super class [Fragment] by default must extend this class instead of [Fragment]
 */
abstract class BaseFragment(
    @LayoutRes resId: Int,
) : Fragment(resId) {

    protected open val navigator get() = requireActivity() as Navigator

    protected open val sharedData get() = requireActivity() as SharedData

}

Methods can be used:
abstract class BaseFragment(
    @LayoutRes resId: Int,
) : Fragment(resId) {

    protected open fun navigator() = requireActivity() as Navigator

    protected open fun sharedData() = requireActivity() as SharedData

}

How is it better and why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
illuzor, 2021-08-20
@MaxSiominDev

In this case, it doesn't matter at all. You need to use it in the most convenient way.

J
Jacen11, 2021-08-20
@Jacen11

well, it's actually possible

protected open val navigator  = requireActivity() as Navigator

    protected open val sharedData = requireActivity() as SharedData

offset then?
and did not understand why open

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question