확장함수

라이브러리 내부에 있는 클래스와 같은 경우 직접 내부를 변경할 수 없으므로 확장함수를 사용.

class Kong {
    val name = "홍진호"
 
    fun playGame() {
        println("폭풍 저그! 홍진호가 간다!")
    }
	// 일반 함수
	fun speak(sentence: String) {
	    println(sentence)
	    println(sentence)
	}
}

 
// 확장 함수
fun Kong.speak(sentence: String) {
    println(sentence)
    println(sentence)
}

연산자 오버로딩, 위임프로퍼티

연산자 오버로딩

클래스 내부에 선언하거나 확장함수로 선언가능

// operator overloading - member function
data class Point(val x: Int, val y: Int) {
	operator fun plus(other: Point) : Point {
		return Point(x + other.x, y + other.y)
	}
}

// extension function
operator fun Point.plus(other: Point) : Point {
		return Point(x + other.x, y + other.y)
}

이항 연산자

함수 이름 복합연산자
a * b times timeAssign
a / b div divAssign
a % b rem remAssign
a + b plus plusAssign
a - b minus minusAssign

연산자 우선 수위 적용

산술 연산자와 복합연사자를 동시에 정의하면 컴파일러 에러 (불변과 가변에 대해서 고려)

단항 연산자

함수 이름
+a unaryPlus
-a unaryMinus
!a not
++a, a++ inc
—a, a— dec

비교연산자

함수이름
== equals
not equals