1. 과부하 이항 연산자
그럼 오버로드 운영자
?! === == 실시간 오버로드 할 수 없습니다가 &&는 || 것을 주
우리는 사용하는 1 + 1
것을 자연에서 실행을 1.plus(1)
우리가 무시할 수 있습니다과 기타 기능을합니다.
(1 + 2I) + (: 다음으로, 우리는 예를 들면, 가산함으로써, 복수의 기본 동작 클래스를 설계 + 4I 3) = 4 + 6I 감산 (1 + 2I) - ( 3 + 4I) = - 2 - 2I는
승산하여, (1 + 2I) * ( + 4I 3) = - 5 + 10I
우리는 다음과 같은 복잡한 클래스는 디자인 :
- 멤버 변수 : 실제 실제 이미지의 허수 부분, 그것은 지능이다
- 회원 방법 : 더하기, 빼기, 곱하기
class Complex {
var real: Int = 0
var image: Int = 0
constructor()
constructor(real: Int, image: Int) {
this.real = real
this.image = image
}
}
추가 규칙은 실제 플러스 실수 부, 허수 부, 감산, 분할 공감 결합 허수 부를 구현
operator fun plus(c: Complex): Complex {
return Complex(this.real + c.real, this.image + c.image)
}
operator fun minus(c: Complex): Complex {
return Complex(this.real - c.real, this.real - c.real)
}
operator fun times(c: Complex): Complex {
return Complex(this.real * c.real - this.image * c.image, this.real * c.image + this.image * c.image)
}
사용
val c1 = Complex(1, 1)
val c2 = Complex(2, 2)
val p = c1 + c2
val t = c1 * c2
2. 과부하 증가 감소 단항 연산자
여기 코드는 우리가 것만 단항 연산자를 알고, 여기 중지 라인에 대신하는 기능 :
- a.unaryPlus ()
+ A - a.unaryMinus ()
-a (부) - a.not ()
! - a.inc ()
++, ++ - a.dec ()
A -, -
3. 과부하 비교 연산자
같은 책 섹션 :
- a.compareTo (b)> 0
A> B - a.compareTo (b) <0
은 <B - a.compareTo (b)> = 0
A> B = - a.compareTo (b) <= 0
은 <= B
4. 과부하 할당 연산자
같은 책 섹션 :
- a.plusAssign (b)
A + B = - a.minusAssign (b)
A - B = - a.timesAssign (b)
A * B = - a.divAssign (b)
의 A / B = - a.remAssign (b)
A = B %의