how to write a kotlin model class
# android
a
how to write a kotlin model class
stackoverflow 6
m
model?? there are data classes in Kotlin which will generate
toString
and
hashcode
methods for you automatically and yeah getter and setters too.
l
Data classes also generate the really useful
copy
and
equals
functions. https://kotlinlang.org/docs/reference/data-classes.html
a
thanks
g
#getting-started
a
private lateinit var customer_address_id: String private lateinit var city: String private lateinit var country_id: String private lateinit var firstname: String private lateinit var lastname: String private lateinit var street: String private lateinit var telephone: String constructor() fun getCustomer_address_id(): String? { return customer_address_id } fun setCustomer_address_id(customer_address_id: String) { this.customer_address_id = customer_address_id } fun getCity(): String? { return city } fun setCity(city: String) { this.city = city } fun getCountry_id(): String? { return country_id } fun setCountry_id(country_id: String) { this.country_id = country_id } fun getFirstname(): String? { return firstname } fun setFirstname(firstname: String) { this.firstname = firstname } fun getLastname(): String? { return lastname } fun setLastname(lastname: String) { this.lastname = lastname } fun getStreet(): String? { return street } fun setStreet(street: String) { this.street = street } fun getTelephone(): String? { return telephone } fun setTelephone(telephone: String) { this.telephone = telephone }
g
Please, check data class docs
You need something like this:
Copy code
data class User(var city: String, var telephone: String)
💯 2
and consider use #getting-started You question is not related to Android
g
@Azhar it's like forth time people suggest you #getting-started. IMHO, learning by try and fail, collecting random bits of information from slack threads is going to be extremely slow, You need something more systematic, with lessons and exercises. Any online Kotlin course for beginners will do (Coursera and such).
👍 4
a
ok