
C++에는 있지만 자바에는 없는 것은 무엇이 있을까요?
지금은 많이 비슷(?!)해졌지만 아직까지도 차이점은 존재합니다.
그 중의 하나가 연산자 오버로딩(operator overloading)입니다.
C++는 객체간의 연산을 사용자가 따로 정의하는 방법을 제공하고 있습니다.
그게 연산자 오버로딩인데 자바에서는 지원하지 않고 있죠.
이런 답답한 부분을 그루비에서 시원스럽게 지원해 주고 있습니다.
연산자 관련 메서드
class Money {
int amount
String currency
Money( _amount, _currency) {
amount = _amount
currency = _currency
}
String toString() {
return "$amount$currency"
}
Money plus(Money b) {
return new Money(amount + b.amount, currency)
}
}
def 백원 = new Money(100,'원')
def 천원 = new Money(1000, '원')
println 백원 + 천원
결과
1100원