재사용성 (1) 썸네일형 리스트형 [AOS] Kotlin 확장 함수 자바에서 기존에 생성되어 있는 클래스에 새로운 매소드를 추가하려면 해당 클래스를 상속해서 사용하는 방법밖에 없지만 코틀린은 확장 함수를 사용해 상속없이 가능합니다. 예를 들어 Long타입의 현재 시간을 '2021.10'과 같이 변경해야할때 아래처럼 Utils 클래스의 메소드를 만들어 재사용하곤합니다. class Utils{ fun convertCalendarDate(tme: Long, format: String): String{ return SimpleDateFormat(format).format(Date(tme)) } } 확장 함수로 구현했을 경우 아래처럼 바뀝니다. fun Long.convertStrDate(format: String): String = SimpleDateFormat(format).f.. 이전 1 다음