ããªãã¸ã§ã¯ãæåã®ç¶æ¿ã使ããªãã¨ãã主張ãåºã¾ã£ã¦ããããã§ãããªãã§ãã¡ã«ãªã£ããã§ããããã
ã¤ã³ã¿ã¼ãããã§è¦ããããç¶æ¿ã¯ãã¡ãã¨ãã主張ãããã¤ãçºãã¦ãå人ã¨è°è«ãã¤ã¤ãèãã¦ã¿ã¾ããã
ãã³ã¼ããèªã¿ã«ãããªãã
ç¶æ¿ãããã¨ãã¡ã½ããå¼ã³åºããå®éã«ã©ã®ã¡ã½ããå®ç¾©ãå¼ã³åºãã®ãåé¢ã§ããããªãã ãããã¬ã使ã£ã¦ã親ã¯ã©ã¹ã®ã¡ã½ããã«é£ãã ããåã¯ã©ã¹ã«é£ãã ãããã®ã追ããããªãã¨è¡ããªãã ã¤ãããã¨ãã主張ã
ãã£ã¡ãããããããããã§ãããããã¯ãé«åº¦ã«å ±éåãããã³ã¼ãã¯èªã¿ã«ãããã¨ããä¸è¬çãªå´é¢ãããªã大ãããããªã ãã¨ãã°ç¶æ¿ã®ä»£ããã«é«éé¢æ°ã使ãã¨ãé¢æ°å¼ã³åºããã©ã®ã¯ãã¼ã¸ã£ã«é£ã¶ãåé¢ã§ããããªããªãã ã²ã©ãã¨ã³ã¼ã«ããã¯å°çã«ãªã£ã¦ä½ãä½ããã«ãªãã¾ãã
ç¶æ¿ããã¨ããã«ã¾ããçç±ãæ³åããã¨ããã¹ã¦ã®ã¡ã½ããå¼ã³åºããããªã¢ã¼ãã£ãã¯ã«ãªãããã®ã¯ãèªã¿ã«ãããå¢ãã¦ãããã é«éé¢æ°ãªããé«éé¢æ°ã®å¼ã³åºãã ã注æããã°ãããããC++ ã® virtual ãã¼ã¯ã¼ãã¯æ£ããã£ãã®ã ï¼ï¼ï¼ã
ããããã°ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ç¶æ¿ã¯è¯ãããå®è£ ã®ç¶æ¿ã¯ãã¡ãã¨ããæ´¾é¥ãè¦ããã¾ããããã®åé¡ã¯ãã¾ãå¤ãããªããã§ãã ãªããªããé£ã³å ã®åè£ãã親åã¯ã©ã¹ã ãããããã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ããå ¨ã¯ã©ã¹ãã«ãªãã®ã§ãæªåããå¯è½æ§ãã
ãããç¥ã¯ã©ã¹ã«ãªãã
åºåºã¯ã©ã¹ã«ã¤ãã¤ã便å©ã¡ã½ãããçããã¦ãã¾ããæ°ã¥ãã°ä¾¿å©ã¡ã½ãããå¯ãéãããç¥ã¯ã©ã¹ãã«ãªã£ã¦ããã ãã®ä¾¿å©ã¡ã½ãããã¡ã¯ã誰ã使ã£ã¦ããããããªãã®ã§ãå¤æ´ã§ããªããªã£ã¦ãã¾ãã ã¤ãããã¨ãã主張ã
ååï¼å¯ãéãï¼ã¯ãããããã ã³ã³ãã¸ã·ã§ã³çã§åãåºããã¨æã£ã¦ããæ¢åã®ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ã¢ã¯ã»ã¹ããå¿ è¦ããã£ããããã¨ãã¤ãã¤ãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã«ãã¦ãã¾ã£ããã ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããæé»çã« this/self ãåãåãã容æã«ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã«ã¢ã¯ã»ã¹ã§ãã¦ãã¾ãã®ã¯ãç¶æ¿ãå¼·åãããããã¨ãããæ°ã¯ãã¾ãã
ãã ããç¥ã¯ã©ã¹ããä½ã£ã¦ãã¾ãè ã¯ãç¶æ¿ãç¦æ¢ããã¨ããã¹ã¦ãè©°ãè¾¼ãã åä¸ãç¥ã¢ã¸ã¥ã¼ã«ããè²æããããããªãã®ããªã
å¾åï¼å¤æ´ã§ããªããªãï¼ã¯ãã³ã¼ãå ±éåãã®ãã®ã®åé¡ã§ãç¶æ¿ä»¥å¤ã§ãèµ·ããæ°ããã¾ãã ããããªã¨ãããã使ããã¦ããã³ã¼ããå¤æ´ããã®ã¯ã©ãããã£ã¦ããã©ãã é¢æ°ã§åãåºãã¦ããmix-in ã«ãã¦ããã³ã³ãã¸ã·ã§ã³ã»å§è²ã§åãåºãã¦ããããã¯æ¬è³ªçã«ã¯ããããªãã
ç¶æ¿ã ã¨ç¹å¥ããã©ããªãçç±ãããã ãããããã¾ãæãã¤ããªãã£ãã ããããã¤ã³ã¹ã¿ã³ã¹å¤æ°ã®ã¢ã¯ã»ã¹æ§ç¢ºä¿ã®ããã«ãåãåºãããããããé¢ã¯ããããªãï¼
ãå¤ãªç¶æ¿ããã人ãããã
ã³ã¼ããåå©ç¨ããããã ãã«ã親åé¢ä¿ã¨ã¯è¨ããªãç¶æ¿ããã¦ãã¾ãããã°ã©ããããã æããã®ãæªç¨ãã¦ãã¾ãç¶æ¿ã¯ç¦æ¢ãã¹ãã ãã¨ãã主張ã
幸ããæå³çã«ãããªãã¨ãããããã°ã©ãã¯èº«è¿ã«ããªãã®ã§ãæåã¯ãã³ã¨ãã¾ããã§ããã ã§ããããããç¶æ¿é¢ä¿ãçã¾ããã·ããªãªã¯æãã¤ãã¾ããã
- ãã¨ãã¨
Clock
ã¨ããã¯ã©ã¹ã§ã¢ããã°æè¨ãå®è£ ãã¦ããã - ãã¸ã¿ã«æè¨ãå®è£ ããããªã£ãã
Clock
ãåºåºã¯ã©ã¹ã«ããAnalogClock
ã¯ã©ã¹ã¨DigitalClock
ã¯ã©ã¹ãæ´¾çãããã¨ããããã- ã¨æã£ããããã§ã«ããããªäººã
new Clock()
ãã¦ãããå ¨é¨new AnalogClock()
ã«æ¸ãæãã¦ãããï¼ ãã¼ãâ¦â¦ - äºææ§ãéè¦ãã¦ããããªã
DigitalClock extends Clock
ã§å¦¥åããã - ããã¸ã¿ã«æè¨ãã¢ããã°æè¨ãç¶æ¿ãããã¨ããçµæã ãè¦ã人ã¯ãæ¸ãã人ããã«ã ã¨æã£ã¦ãã¾ãã
ã¨ãããã¨ã§ãäºææ§ã®é æ ®ãªã©ã®äºæ ã§ãå¥å¦ãªç¶æ¿é¢ä¿ã¯ç¢ºãã«çããããã¨ãããã身ã«è¦ããããã
ãã ãããã¯ç¶æ¿ä»¥å¤ã§ãæ®éã«èµ·ããã
ã¢ããã°æè¨ãä½ã create_clock()
ã¨ããé¢æ°ãå
¬éãã¡ããã¨ãå¾ãã create_analog_clock()
ã«ããã®ã¯å¤§å¤ã
æªæ¥ã®ã¦ã¼ã¶ã¯ããªã㧠create_clock()
ãã¢ããã°æè¨ãªã®ããã«ãªã®ãã£ã¦æãã®ããããã¾ããã
ï¼æè¨ã®ä¾ããã£ããããªã人ã¯ãã¢ãã¯ããã¬ãã»ã«ã©ã¼ãã¬ãã¨ããã¬ãããã ä¸è¦§ãã好ããªä¾ãé¸ãã§èªã¿æ¿ãã¦ãã ããï¼
ç¶æ¿ããã¨ããã«åé¡ã ã¨ããããå¾ç¥æµã ã¨è¯ãè¨è¨ãè¦ããããããããªã£ã¦ããªããã¨ãæ°ã«ãªããããã®ããã é¢æ°ãªããã¯ãã¯ããã£ã¦æµãããã©ããªãã¸ã§ã¯ãæåã¯ãã°ãã«ãªã£ã¦ãã¦ãå¥å¦ãªè¨è¨ãè¦ããã¨ã«èããããªãã ã§ãããã§ç¶æ¿èªä½ãç¦æ¢ãããã¨ããã®ã¯ããã¯ããªãã¸ã§ã¯ãæåã¸ã®åå·çãªææ ã£ã¦æãããã¾ããâ¦â¦ã
ã¾ã¨ã
ãç¶æ¿ã¯ãã¡ãã¨ããçç±ã§ãããç¨åº¦å ±æããã®ã¯ãã¾ã®ã¨ãã2ã¤ã
- ãã¹ã¦ã®ã¡ã½ããå¼ã³åºããããªã¢ã¼ãã£ãã¯ã«ãªãããã®ã§ãã©ãã«é£ã¶ã常ã«æ³¨æãå¿ è¦ã§ãã³ã¼ãèªè§£ã大å¤ï¼ãã¾ã©ããªãã¨ãã£ã¿æ¯æ´ã§ãªãã¨ããªããããªï¼
- æé»çã« this ãåãåãã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¯ä¾¿å©ããã¦æªç¨ãããããå¯ãéãã®ãç¥ã¯ã©ã¹ãã«ã¤ãªãããã¡ï¼ãã ãç¶æ¿ç¦æ¢ã§æ¬å½ã«è§£æ±ºãããã¯ããããªãï¼
ãç¶æ¿ã®åé¡ã¯ãããªäºæ«ãªãã¨ãããªãï¼ ãã£ã¨å¤§ããåé¡ã ï¼ãã¨ãã人ããããããã²ããããæãã¦ä¸ããã