Javaããã¸ã§ã¯ãã§Groovyãå°å ¥ãã¹ãï¼ã¤ã®çç±
å段
TDDBootCamp in Tokyo 1.5ã«Javaã°ã«ã¼ãã®ä¸å¡ã¨ãã¦åå ããã¦ããã ãã¾ããã
ããã§Groovyãããããã¨ããããããªã¨ã³ã¸ãã¢ã¨åºä¼ãã¾ãã¦ãGroovyã§TDDãããã¦ããã ãã¾ããã
ãããªãGroovyã§ãããã¯ããæ¸ãäºã¯ãªããªããªãã¨æã£ã¦ããã®ã§ãJavaã®ãããã¯ãã³ã¼ãã«å¯¾ãã¦ãGroovyã®ãã¹ãã³ã¼ããæ¸ããã¨ããæ¹æ³ã§æ¼ç¿ãã¾ããã
ããã§ãçæ§ãGroovyã¸å¤å°ãªãã¨ã注ç®ãã¦ãã ããã¾ããã®ã§ããã®ã¨ã³ããªã¼ãæ¸ãã¦ã¿ããã¨æãã¾ããã
Javaããã¸ã§ã¯ãã§Groovyãã¹ãã³ã¼ããå°å ¥ãã¹ãï¼ã¤ã®çç±
以ä¸ã§ãããï¼ã¤ã®ãã¤ã³ãã¯ãGroovyãç¥ããªãJavaããã°ã©ãã¼ãããã«å§ããããGroovyã®å¼·åãªæ©è½ããã¨ãããã¾ããã
ãã¡ããããã«ããã以å¤ã«ãããããã®Groovyã®å¼·åãªæ©è½ã¯ããã¾ããããããã®å¨åãç¥ãã®ã¯Groovyã«æ¬è
°ãããã¦ããã§ãé
ãã¯ããã¾ããããªã«ããGroovyã¯Javaã¨åãããã«æ¸ãã¦ãã»ã¨ãã©åãããã«åä½ãããã¹ãã³ã¼ãã¯æ°è»½ã«æ¸ãããã®ã§ãããã¨ãå¤ãããã§ãã
1.ç°¡æ½ãªã«ã¼ãå¦ç
2.æ示çãªPowerAssert
3.privateã«ãç°¡åã«ã¢ã¯ã»ã¹ã§ãã
4.å¼·åãªãããã°ããªã³ã
5.ç°¡æ½ãªãªã¹ãããããå®ç¾©
1.ç°¡æ½ãªã«ã¼ãå¦ç
Javaã®å ´å
for(int i=0; i < 5; i++){ System.out.println(i); } List<String> list = new ArrayList<String>(); // listã«è¦ç´ 追å å¦ç for(String str : list){ System.out.println(str); }
Groovyã®å ´å
5.times{ println it } List<String> list = new ArrayList<String>(); // listã«è¦ç´ 追å å¦ç list.each{ println it }
ãããã¯
0.upto(4){ println it }
Groovyã§ã¯ æ°å.times ã§ãã®æ°ååã®ã«ã¼ãå¦çãå®è¡ãã¾ãããã®ã¨ãã«ã«ã¼ãå¦çã®ä¸ã§ä½¿ããã«ã¦ã³ã¿ã¯0ãªãªã¸ã³ã§ãã
åãããã«Listã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯eachã¡ã½ããã§foreachå¦çã使ç¨ã§ãã¾ãã
ãããéã1ãªãªã¸ã³ããã®ä»ã§è¡ãã¨ãã«ã¯ éå§æ°å.upto(çµäºæ°å) 㧠éå§æ°åããå§ã¾ãã«ã¼ãå¦çãå®è¡ã§ãã¾ãã
ã«ã¼ãå¦çã®ãªãã® it ã¯èªåçã«ãã®å¤æ°åã§ã«ã¦ã³ã¿ã代å
¥ããã¦ãã¾ããâ»itã¯ã¯ãã¼ã¸ã£ã®ããã©ã«ãå¤æ°ã§ãããããã¯ã¯ãã¼ã¸ã£ãä½ã§ããããå¦ãã§ãããã¨ãã«ç解ããã°åé¡ããã¾ããã
2.æ示çãªPowerAssert
assert String.class.name.equals(Integer.class.name)
Groovyã§ã¯ãã¼ã¸ã§ã³1.7ããassertæã®è¡¨ç¤ºãé常ã«ç¶ºéºã«ãªãã¾ããããã¹ãã失æããæã«ã³ã³ã½ã¼ã«ãããã¨è¦ã§ãããã®ã¯ç´ æ´ãããã§ãã
3.privateã«ãç°¡åã«ã¢ã¯ã»ã¹ã§ãã
class Hoge { private String foo = "this is private." public String bar = "this is public." } Hoge hoge = new Hoge() println hoge.foo println hoge.bar
å®è¡çµæ
this is private. this is public.
TDDããªãã¡ã¯ã¿ãªã³ã°ãã¬ã¬ã·ã¼ã³ã¼ãã§ãã¹ãã³ã¼ããæ¸ãã¨ãã«privateã«ã¢ã¯ã»ã¹ããããªãã¨ããããã¾ããç°¡åã«æ¸ããã®ã§ã¨ã¦ãããã§ãã
4.å¼·åãªãããã°ããªã³ã
println new HashMap().dump()
å®è¡çµæ
<java.util.HashMap@0 table=[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null] size=0 threshold=12 loadFactor=0.75 modCount=0 entrySet=[] keySet=null values=null>
dump()ã¡ã½ããã¯Groovyã®ããã©ã«ãã¡ã½ããã«ãªã£ã¦ããã®ã§ãã©ã®ãããªã¯ã©ã¹ã«ã§ãåºæ¬çã«ä½¿ãã¾ããå
é¨ã®ãªãã¸ã§ã¯ãã®ç¶æ
ãå¯è½ãªéããã£ã¼ã«ãåã¨å¤ãåºåãã¦ããã¾ãã
5.ç°¡æ½ãªãªã¹ãããããå®ç¾©
List foo = [1,2,3,4,5,6,7,8,9,0] foo.each{ println it } Map bar = [hoge:"value1",fuga:"value2"] bar.each{ println it println it.key println it.value }
å®è¡çµæ
1 2 3 4 5 6 7 8 9 0 hoge=value1 hoge value1 fuga=value2 fuga value2
[a,b,c,d]ã§ArrayListã®ã¤ã³ã¹ã¿ã³ã¹ãçæã§ãã¾ãã[key1:value1,key2:value2]ã§LinkedHashMapã®ã¤ã³ã¹ã¿ã³ã¹ãçæã§ãã¾ãã
ä»ã«ãListã¯#add()ã®ä»£ããã« list+"hoge" ã®ããã«+ã§è¿½å ã§ããã¨ãããããããã¾ãããã¨ããããä¸è¨ï¼ã¤ãå®è·µã§ããã ãã§ãååã«Javaã®ã³ã¼ããããç´æçã«ãªãã®ã§ã¯ãªãã§ããããï¼
Happy Groovy Life!
- ä½è : é¢è°·åæ,ä¸å潤äº,é æ±ä¿¡æ´,ä¸ééæ²»
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2011/07/06
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 6人 ã¯ãªãã¯: 392å
- ãã®ååãå«ãããã° (155件) ãè¦ã