toString()
메서드는 객체를 문자열로 표현하여 반환한다.print()
메서드를 사용할 때는 생략해도 상관없다.hashCode()
메서드는 객체의 해시 코드를 반환한다.operator ==()
메서드는 객체의 동등성을 비교하는 연산자다.==
자체가 메서드 명이다.)==()
보다 비용이 싼 hashCode
비교를 사용한다.operator ==(Object other) =>
identical(this, other) ||
other is Book &&
runtimeType == other.runtimeType &&
_title == other._title;
int get hashCode => _title.hashCode ^ _publishDate.hashCode;
bool
Dart에서 sort()
함수는 Collection 내의 요소를 정렬하는데 사용되는 메서드다.
sort()
함수는 내부 요소들은 Comparator
를 구현해 비교함수를 제공받아 요소의 순서를 결정하는데 사용한다.
💡 names.sort(a , b) ⇒ a.compareTo(b);
Comparator
함수는 두 개의 요소를 비교하여 정렬 순서를 결정하는 규칙을 정의한다.
Comparator
함수는 익명 함수나 람다 함수로 작성된다.
Comparator
함수는 두 개의 인수를 받는다.
/// 깊은 복사
Book copyWith({
String? title,
DateTime? publishDate,
String? comment,
}) {
return Book(
title: title ?? _title,
publishDate: publishDate ?? _publishDate,
comment: comment ?? _comment,
);
}
// copyWith를 사용하여 깊은복사
final Book copyBook = originalBook.copyWith(
title: '복사본',
publishDate: DateTime(2023, 12, 11),
comment: '이 책은 ${originalBook.title}의 복사본이다.');