연산자 오버로딩

인덱스로 원소에 접근 - Map 원소 접근시 괄호([]) 사용 코틀린에서는 맵의 원소에 접근할 때 자바에서 배열 원소에 접근하는 각 괄호([])를 사용할 수 있다. 인덱스 연산자는 get/set 관례를 따르며, Map과 MutableMap 인터페이스에는 이미 두 메서드가 들어가 있다. val value = map[key] mutableMap[key] = newValue 구현 예시 operator fun Point.get(index: Int): Int { return when(index) { 0 -> x 1 -> y else -> throw IndexOutOfBoundsException("Invalid coordinate $index") } } val p = Point(10, 20) println(p[1]..
깡냉쓰
'연산자 오버로딩' 태그의 글 목록