짬짬이 그루비Groovy를 살펴보기 시작했다. 순 그레들gradle을 능숙하게 다루기 위한 목적으로 살펴보고 있는데,
그루비가 가지고 있는 매력도 상당하다.
- Rocking The Groovy - github
 신기하다잉!? +_+)?
Type plus(Type type) {} 를 재정의하니까 다른 형태로 동작한다.
package kr.pe.ihoney.groovy.study.begin
class Money {
    private int amount
    private String currency
    Money(int amount, String currency) {
        this.amount = amount;
        this.currency = currency;
    }
    /**
     * 재정의된 plus(+) 연산자
     * @param other
     * @return
     */
    Money plus(Money other) {
        assert null != other
        if(other.currency != currency)
            throw new IllegalArgumentException("Cannot add $other.currency to $currency");
        return new Money(amount + other.amount, currency);
    }
    @Override
    public int hashCode() {
        amount.hashCode() + currency.hashCode()
    }
    @Override
    public boolean equals(Object other) {
        if(null == other) return false
        if(!(other instanceof Money)) return false
        if(currency != other.currency) return false
        if(amount != other.amount) return false
        return true
    }
}
package kr.pe.ihoney.groovy.study.begin;
import static org.junit.Assert.*;
import org.junit.Test;
class MoneyTest {
    Money money
    @Test
    public void test() {
        def bulk = new Money(1000, "WON")
        assert bulk
        assert bulk == new Money(1000, "WON") //재정의된 ==(equals) 연산자 사용
        assert bulk + bulk == new Money(2000, "WON") // 재정의된 +(plus) 연산자 사용
    }
}'Programming' 카테고리의 다른 글
| 리눅스(우분투)에서 GVM을 설치하고 개발환경을 간결하게 해보자. (2) | 2014.09.26 | 
|---|---|
| [Github] 단축키 - [T] File Finder (0) | 2014.04.28 | 
| 그루비 프로젝트를 이클립스에서 임포트할 때 빼먹지 말자, apply plugin: 'eclipse' (0) | 2014.03.20 | 
| 스프링캠프SpringCamp 2013이 열립니다. (0) | 2013.09.22 | 
| 터미널 프롬프트에서 깃의 브랜치 이름을 확인하자. (0) | 2013.08.22 | 


