ãããã°ã©ã ã«ããã型システムã£ã¦ã®ã¯æ°å¦ã»è«çå¦ã®型理論ãåºç¤ã«ãã¦ãã¦ãæ½è±¡çãªããããã¨ãã代ç©ããã®åã·ã¹ãã ã®çè«ãç¾å®ã«å®è£ ãããã®ã¨ãã¦ãå¤æ§ãªããã°ã©ã è¨èªãåå¨ããããã ã
ãé¶ã¨åµã¨ãã話ã ã¨ã©ã£ã¡ãªãã ãããå®è£ ãå ã§ãããçè«ã¨ãã¦ã¾ã¨ããã£ã¦æããªã®ããçè«ãã¾ãèãããã¦ããã°ã©ã è¨èªãå®è£ ãããã®ãâ¦ãæåã®ããã°ã©ã è¨èªã¨ããã¨1954å¹´ã«ç»å ´ããFORTRANã§ãIBMã®ã¸ã§ã³ã»ããã«ã¹æ°*1ã«ãã£ã¦èæ¡ããããFORTRANã«ã¯ããã¤ãã®æ°å¤åãããããã®é ã«åã·ã¹ãã ã®ä½ç³»çãªçè«ãã§ãã¦ããã¨ã¯æããªãããå¤åãå®è£ ãå è¡ãªãã ãããªã
çè«ã¨ãã¦ã®åããã®Javaå®è£ ã®ã¯ã©ã¹
ãåã·ã¹ãã (type system)ã§ããå(type)ã¯åçè«ã«åºã¥ãããã§ãæ°å¦çãªæ½è±¡æ¦å¿µã«è¿ããä¾ãã°åããæ¥å¸¸çæ´»ã§æ°å¦çæ¦å¿µã§ãã1/3ã¨ããå¤ãå³å¯ã«ä½¿ããã¨ã¯ãªãããåºæ¥ãªããã§ã1/3ã¨ããæ°å¦çæ¦å¿µã¯å®è£ ãæã£ã¦è¡ä½¿ããããã¶ã£ã¡ããã¦è¨ãã¨ãåãã¯ã±ã¼ããï¼äººã«åé ã§ããã
ãããã«æ°å¦çãªå³å¯ãã¯å½ç¶ãªãããã ãã©ãå®ç¨ä¸ã¯å°ããªããåããããã°ã©ã è¨èªã¨ãããå®è£ ããéãã¦è§¦ãããåãã£ã¦ã®ã¯ãæ¥å¸¸çæ´»ã®1/3ã®ãããªãã®ã§ãæ°å¦çãªãåãã§ã¯ãªãã¦ãåãã®è¨èªå®è£ ã¨ããå½±ãªãã ã*2
ãJavaã¨ããè¨èªã®ãåãã¯åã·ã¹ãã ãå©ç¨ãã¦å®è£ ãããå·¥å¦çãªææç©ã§ãæ°å¦çãªãåãã¨ã¯éã£ã¦ãã¾ã£ã¦ããããJava::åãã¨ãåã·ã¹ãã ::åãã¯éããã ããããJava::åãã¨ããã¨ãã¾ãããªããã£ãå(PrimitiveType)ã¨åç §å(ReferenceType)ã¨ããï¼åå²ããå§ã¾ãã¢ã¬ã§ãåç §åã¯ããã«ã¯ã©ã¹å(ClassType)ã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹å(InterfaceType)ã¨é åå(ArrayType)ã«åãããã*3
ãã¯ã©ã¹åã®å®è£ ã®è¨è¿°ãã¯ã©ã¹å®£è¨(ClassDeclaration)ã¨è¨ãã*4é常ãã¯ã©ã¹ãã¨è¨ã£ãå ´åã«ãã®ã¯ã©ã¹å®£è¨ã«ãã£ã¦è¦å®ãããã¯ã©ã¹åã®ä¸å®è£ ãæãã¦è¨ããã¨ãå¤ãã ããã
ãJavaã«ã¯ãªãã¬ã¯ã·ã§ã³ã¨ããè¨èªæ©è½ããã£ã¦ããããã®ã¯ã©ã¹ãããã°ã©ã ä¸ã§æ½è±¡çã«æ±ãããã®ã¯ã©ã¹ç¾¤ãç¨æ(java.lang.reflectããã±ã¼ã¸)ããã¦ãããjava.lang.Classã¨ããã¯ã©ã¹ã¯ãªãã¬ã¯ã·ã§ã³æ©è½ã®ãã¡ãã¯ã©ã¹å®£è¨ã表ç¾ããã¯ã©ã¹ã ãå®éã®ä¼è©±ã§ããã¯ãClassã¯ã©ã¹ã(ã²ãããªã§è¡¨è¨ããã¨ãããããããã)ã¨å¼ã°ããããããããããã
ãçµå±ããã¯ã©ã¹ãã¨ããç¨èªã«è¨èªã横æãã確ããå®ç¾©ã¯ãªãã¦ããâè¨èªã«ãããã¯ã©ã¹ãã¨ããããåã·ã¹ãã ::åãã®å®è£ ã¨ããåã®å½±ã¨ããæãæ¹ãããããããªããã ã¨æãã
ãクラスとは何か - SiroKuro Pageã®ç¨èªã®æ··ä¹±ããããã解ãã»ãããã ãããã
ãã®åã¯ä»£å½¹ãåã¾ãã
ãåã·ã¹ãã ::åã®è©±ã«æ»ã£ã¦ãããåã«ä»ã®åã®ä»£å½¹ãåã¾ããã©ãããã¨ãã話é¡ãèãããã
ãå³å¯ããæ±ãããªããããåãå¤ããè¦ãå ´åã®å§¿ãå¤é¨ä»æ§ã¨è¨ãããããããã«åã®ã¡ã³ãã¯å°ãªãã¨ãåç以ä¸ã«ç¨æããã¦ããªããã°ãªããªããHDMI端åããä»ãã¦ããªã24inchã¢ãã¿ã¯14inchã¢ããã°ãã¬ãã®ä»£å½¹ãåãããã¨ãã§ããªããã ãã¹ã¼ãã¼ãã¡ãã³ã³ã¨ãç¹ãããªããã ããé«æ©è½ãªã®ã¨ãåãã¨ãã¦ä»£å½¹ãã§ããã®ã¯éãã£ã¦ãã¨ã ã
ãããã§ãå³å¯ã«å ¨ã¦ã®ç«¯åãåãã¦ãã¾ãï¼ã£ã¦è¬³ããããããã¯è¦åãçºè»ã§ç½®ãæãã¦ã¿ã¦ãç´ ç¥ãã¬é¡ã§ä»£æ¿ããã¦åãã¤ã¥ããå®éã«ã³ã³ãã¸ããå ¥å端åãæ¥ç¶ãããã¨ããã¨ãã«å°ã£ã¦ã¨ã©ã¼ã§ããã¨ããï¼ç¨®é¡ã®å®è£ ãé¸ã¹ã¦ãåè ãéçãå¾è ãåçãªåä»ãã®è¨èªã
ãåçè¨èªã¯æ¬ é¥ããã£ã¦ãããã«ééããªããã°ããããããä¿éºã«å ¥ã£ã¦ãªããã£ã¦äºãèµ·ãããªããã°åé¡ãªããããçãªæ¥½è¦³æ§ããã£ã¦å人çã«ã¯å«ã*5ãªãã ãã©ãäºãèµ·ãããªãå¹³ç©ãªæ¥å¸¸ã«ããã¦ã¯ãã®ããããããµãã§ããããããªãã®ãã¨ããã®ãåããæ°ã¯ããã
ããã§ã代役ãã§ãã¾ãï¼ã¨è¨ã£ã人ã«ä»£å½¹ããããã¦ãã¡ããã¨ä»£å½¹ãåã¾ããã©ããã£ã¦ã®ã¯ãããã¦ã¿ãªããã°åãããªãã¦ãéçè¨èªã®å ´åãããã¾ããã®ã¡ã³ãæã£ã¦ãªãããããã¨ãããã¨ã«ã¯ãªããªããã®ã®ããã¸ã«ã«ã¨ã©ã¼ãèµ·ãããã¨ã¯å½ç¶ããããã
åçåã®ãããµãæ
ãåçåã®åã¯åã·ã¹ãã ããããµããªæãã«è¼ããæãããªãã¸ã§ã¯ããä½ãããæã«ã¯ãã£ãé¢æ°ãªã®ã«ã使ããã¨æã£ãããªããªã£ã¦ãã¨ããããã¨ããããActionScript3ãããã®åã¨ãéçã«ãããã¨ããã¯éçãã§ãã¯ãã¤ã¤ãXMLãªãã©ã«ã¨ãã®éçåä»ãããããããªãã¨ããã¯ã¨ããããã³ã³ãã¤ã«ã¯éãã¦ããã¦å®è¡æã«è½ã¨ãã¨ãããããµãæã
ãåã®ä»£å ¥äºææ§ã®ãã§ãã¯ãçãã«ãã¦ããã¦ã確å®ã«ééãã ãï¼ã£ã¦ã¨ãã ãå¼¾ããã§ãå³å¯ããæ±ãããã¾ãã«æ¸é¡æ¸ãã®ã大å¤ã§ãã¼ã¿ãããªãã¨ã«ãªããªãç¨åº¦ã«å¤§éæã«éãã¦ãã¾ããã£ã¦ãã¨ãã§ããéã»åã®ã°ã©ãã¼ã·ã§ã³ã®ãã ãªãã«ãããããªä¾ã ã¨æãã
ããã£ã¨ãJavaã ã£ã¦å®å ¨ã«å³å¯ã«åãã§ãã¯ã§ããããã§ããªãããå¼·ãåä»ããã¨ããããã¯ãå¼·ãã®åä»ããã¨è¨ã£ãæããã ããåä»ããªãã¦ã®ã¯ã¾ã£ãããªãããå®ç§ã¾ã§ã®ã°ã©ãã¼ã·ã§ã³ããã£ã¦ãè¨èªã«ãã£ã¦ãã®ä½ç½®ãéãç¨åº¦ã®è©±ãªããããªããã¨æããã ã
ãã ãããåãã¨ããç¨èªããåã·ã¹ãã ::åããªãæ°å¦çãªå®ç§ãåæã«ãã¦ããã ãããã©ããç¹å®è¨èªã®æ©è½ãæã£ã¦ãã¦ãããã¯åãããã¯åãããªãããªãã¦ç·å¼ãã試ã¿ã¦ããã³ã»ã³ã¹ãªããããªãã ãããã
åã®äºææ§ã¨ããã°ã©ã ã¨ãã¦ã®æåã®ã®ã£ãã
ãhttp://nazakki.blogspot.com/2009/03/blog-post_06.htmlã§æãããã¦ãããããªä¾ããè¨è¨ã¨ãã¦ãã¾ãæ´»ç¨ããã®ãGoFãã¶ã¤ã³ãã¿ã¼ã³ã®TemplateMethodãã¿ã¼ã³ããããã®abstractã¡ã½ãããããè¨ãé ã«ãã³ãã¬ã¼ãããå¼ã°ãã¾ããã¼ã£ã¦ç´æäºããã£ã¦ãããã«åãããå®è£ ãããªãã¡ããããªãããåãã¨ãã¦ã¯äºææ§ããã£ãã¨ãã¦ãããã³ãã¬ã¼ãã®ãç´æã«åããä½ãã«ãã¡ããã¨å¤ãªåããããããããããã ããã
ãJavaã®å ´åããã¹ã¦ã®ã¯ã©ã¹ã¯java.lang.Objectåããã®æ´¾çã¨ãããã¨ã«ãªããã©ãããã®Objectåã£ã¦abstractã§ã¯ãªããã®ã®ãããã¤ãã®ã¡ã½ãããå®ç¾©ããã¦ããã
ãã¤ã¾ãã¨ãããJavaã¨ãããã¬ã¼ã ã¯ã¼ã¯ã«ãããTemplateMethodãã¿ã¼ã³ãªãã ãããJavaã§ããã°ã©ã ãã以ä¸ã¯ããç´æã£ã¦ãã¤ãå®ããªãã¦ã¯ãããªããããã°ã©ã ãæ¸ãã¨è¨ããã¨ã¯ãèªç±ãªããã§ãã¦ãããã°ã©ã è¨èªã®è¦å®ããã«ã¼ã«ã¨ããæ ã®ä¸ã§ç´æãå®ã£ã¦è¡åããªããã°ãããªãã¨ããç¨åº¦ã®ä¸èªç±ãããã
ãåã·ã¹ãã ãè¦å®ããã®ã¯ç©ççãªä»£å ¥äºææ§ã®è¦å®ããã¾ãåãããã«ç´æäºã§éç¨ãããã£ã¦ã®ãå¥ç´ã«åºã¥ãããã°ã©ãã³ã°(programming by contract)ãç©çã¯æ®ºäººãç¦æ¢ããªããã©ã社ä¼ã¯æ®ºäººãç¦æ¢ãããããªãã®ããã§ãããã¨ããã£ã¦ãããã¯éãã
ãã¬ã¼ã ã¯ã¼ã¯ãè¨è¨ããã¨ãã«èãããã¨
ãèªåã¯ä»äºã§ã¯ãã¬ã¼ã ã¯ã¼ã¯çãªãã®ãä½ã£ã¦ããã®ã ãã©ããã¯ãçæ³ã¨ãã¦ã¯ãåã«ããå¶ç´ãï¼ãå¥ç´ã«åºã¥ãããã°ã©ãã³ã°ãã«ãªã£ã¦ãããã¨ã ã¨æãã
ããã¥ã¼ãã³ã¨ã©ã¼ã®å¯¾çã¨ãã¦çæ³çãªã®ã¯ãééãããã¨ãã§ããªããã¨ããã®ããã£ã¦ã誤ã£ã¦ãªã¼ãã¼ã©ã¤ããã¦å¤ãªåããã¦ãããããã¨ãè¨ã人ãããªãããã«ãããã¨ããã*6
ãåã·ã¹ãã ã¯åã®ä»£å ¥äºææ§ãä¿è¨¼ãã¦ããããã©ãç¾å®ã®ããã°ã©ã ã ã¨å®è£ ã³ã¼ããã©ããã«è¼ããªãã¨ãããªããJavaã§interfaceã«ããè¯éºãªç¶æ¿é層ãè¨è¨ããã¨ãã¦ããimplementsããã¨ãã«å®è£ ã³ã¼ãã¸ã®å§è²ãæ³¥ãããããã¼ã¨ãããã¨ã«ãªããã¨ãããã*7
ãåã®å¶ç´ãå©ç¨ãã¦ããã¾ã«ä¾¿å©ãªæ çµã¿ãä½ãããèãã¦ããã¨ãã©ãã«ãç¾è¡ã®Javaã®åã·ã¹ãã å®è£ ã§ã¯å¼±ãã¨ããããã£ãããã¦æ©ããã¸ã§ããªã¯ã¹ã¨ã使ã£ã¦ãã¨ã»ããã³ãç®ç«ã¤ãã ããªãâ¦ã
ã¾ã¨ã
ããããªããã§
- åçè«ã»åã·ã¹ãã ãããã®æ°å¦çãªå®ç¾©ã®ãåãã¨ããã®ããã
- åããããã°ã©ã è¨èªã§è§¦ãããåãã£ã¦ã®ã¯ãã®å®è£ ã¨ããå½±ã
- ããåã®ä»£å½¹ã¨ãã¦åãããæã«åã®ä»£å ¥äºææ§ã«æ¯è¼çå³å¯ãªã®ãJavaã¨ãã®åã·ã¹ãã å®è£
- ãããã¿ã¤ãåOOPã¯ä»£å ¥äºææ§ã¯å³å¯ã«ãããªãã§åãã°ãããããçå®è£
- åã¨ãã¦ä»£å
¥äºææ§ãããã¨ãããã¨ã¨ãããã°ã©ã ãæå³ã©ããã«åããã¯å¥ã®è©±é¡
- å¥ç´ããã°ã©ãã³ã°ã¯ãã¾ãåãããã«ã«ã¼ã«ãå®ãããããªãã®
- åã·ã¹ãã ã«ããè¦å®ã¨å¥ç´ããã°ã©ãã³ã°ãä¸è´ãã¦ããã¨ééãããããªãã®ã§ãã¥ã¼ãã³ã¨ã©ã¼ç観ç¹ããçæ³ç
*1:John Warner Backus (1924-2007) FORTRANã®çºæè ã¨ç¥ãããã»ããRFCãªã©ã®ãããã³ã«ã®ä»æ§æ¸ã§ã馴æã¿ã®ããã«ã¹ã»ãã¦ã¢è¨æ³ã®çºæè ã§ãããã
*2:2009-03-06 - きしだのはてなã§ãä¼¼ããããªãã¨ãè¨ããã¦ãããã©ãä¸å¿ã®ç©ä½ããã®ã¨ã³ããªã¨ã¯éãã
*3:Javaè¨èªä»æ§ç¬¬3ç 4.1 åã¨å¤ã®ç¨®é¡ã4.3 åç §åã¨ãã®å¤
*4:Javaè¨èªä»æ§ç¬¬3ç 8.1 ã¯ã©ã¹å®£è¨
*5:æ¥åã·ã¹ãã ã ã¨ã¾ãã«ãããéè¦è¦ãããããã§ã
*6:Object#equals()ã®å®è£ ãããã£ã¦Mapã«ãã¼ã¿ãçªã£è¾¼ãã ãæ¶ããã¨ãããããããã°ãããããããã®ã§ãã
*7:åã®å¤éç¶æ¿çãªã·ãã¥ã¨ã¼ã·ã§ã³