Dontsu
01/05/2024, 2:41 AMoverride val
in data class
?
I want to give it a polymorphism to use these data class
more flexible.
For instance,
abstract class CalendarSelectedDate(
open val year: Int,
open val month: Int,
open val day: Int,
) {
open fun getDate(): String {
val calendar = Calendar.getInstance()._toTimeZero_()._apply_ *{*
set(year, month, day)
}
return _dateFormat_.format(calendar._time_)
}
}
class CalendarEndSelectedDate(
override val year: Int,
override val month: Int,
override val day: Int,
): CalendarSelectedDate(
year = year,
month = month,
day = day,
)
class CalendarStartSelectedDate(
override val year: Int,
override val month: Int,
override val day: Int,
): CalendarSelectedDate(
year = year,
month = month,
day,
)
I'm not sure it's good or not because it affects something bad on equals()
and copy()
the only thing i know is that data class
is a class for holding the state. So, if i use data class
and its`copy()` , Does that code violate the principles of a data class?
+ https://stackoverflow.com/questions/38492103/override-getter-for-kotlin-data-classDaniel Pitts
01/05/2024, 2:49 AMDaniel Pitts
01/05/2024, 2:51 AMdata class SelectedDate(val year: Int, val month: Int, val day: Int, val type: Type) {
enum class Type {
START,
END;
}
}
Dontsu
01/05/2024, 2:57 AMif
or when
to clarify objects when i need to add new Type more and more if i use enum class
in this case.Daniel Pitts
01/05/2024, 2:59 AMDontsu
01/05/2024, 3:38 AMCLOVIS
01/05/2024, 8:52 AMabstract class CalendarSelectedDate(
val year: Int,
val month: Int,
val day: Int,
) { … }
class CalendarEndSelectedDate(
year: Int,
month: Int,
day: Int,
) : CalendarSelectedDate(year, month, day)
or declare abstract variables to force the subclass to hold the value:
abstract class CalendarSelectedDate {
abstract val year: Int
abstract val month: Int
abstract val day: Int
}
class CalendarEndSelectedDate(
override val year: Int,
override val month: Int,
override val day: Int,
) : CalendarSelectedDate()
CLOVIS
01/05/2024, 8:54 AMdata class CalendarSelectedDate(
val year: Int,
val month: Int,
val day: Int,
)
@JvmInline value class CalendarStartSelectedDate(val date: CalendarSelectedDate)
@JvmInline value class CalendarEndSelectedDate(val date: CalendarSelectedDate)