객체를 바로 출력해도, 원하는 정보가 나옴!
class 앞에 data 키워드 추가하면, 자동으로 3가지 오버라이딩 됨!
toString()은, 데이터클래스의 생성자에 정의된 프로퍼티만 출력!
hashCode()는, class객체일땐 다른값 리턴, dataclass 객체일때는 같은값 리턴
equals()
→ copy()
data class User(
val name: String,
val profileImg: String,
val age: Int
)
fun UserProcess() {
val user1 = User("Kenneth", "https://store.image/profile_1", 30)
val user2 = user1.copy(name = "scarlet")
// D/user1: User(name=Kenneth, profileImg=https://store.image/profile_1, age=30)
// D/user2: User(name=scarlet, profileImg=https://store.image/profile_1, age=30)
}
// copy()를 이용해, dataclass의 name만 변경하여 쉽게 객체 복사함!
data class Site(val url: String, val title: String) {
val description = ""
}
data class User(
val name: String,
val profileImg: String,
val age: Int
)