ããªã¢ã¼ãã£ãºã ã¯ãªãã¸ã§ã¯ãæåã®3æ¬æ±ã¨ãã¦ãç¶æ¿ããã«ãã»ã«åãã¨å ±ã«èªããããã¨ãå¤ãã§ããããµãã¯ã©ã¹åãã¦ãªã¼ãã¼ã©ã¤ããã¦...ã¨ããã®ã¯ããªã¢ã¼ãã£ãºã ã®ä¸é¢ã«ããã¾ããããã®è¨äºã§ã¯ããªã¢ã¼ãã£ãºã ã¨ã¯ä½ãªã®ãã説æããã¨å ±ã«3種é¡ã®ããªã¢ã¼ãã£ãºã ãåãä¸ãããããªã¢ã¼ãã£ãºã ãã¨ããè¨èãæ§ã ãªè§åº¦ããè¦ã¦ããããã¨æãã¾ãã対象èªè ã¨ãã¦ã¯å ¨ãã®åå¦è ã¨ãããããä½ã¨ãªãããªã¢ã¼ãã£ãºã ã¯ç¥ã£ã¦ã¦ä½¿ã£ããããããã©ã¡ããã¨èª¬æã§ããªãã¨ãããããã人ãèãã¦ãã¾ãã
ããªã¢ã¼ãã£ãºã ãä¸è¨ã§èª¬æãã
ããªã¢ã¼ãã£ãºã ã¨ã¯ãä¸è¨ã§èª¬æããã¨ã©ãããæ¦å¿µãªã®ã§ãããããæ¥æ¬èªè¨³ã¨ãã¦ã¯ããªãã¸ã§ã¯ãæåã®æèã ã¨ãå¤æ æ§ããé¢æ°åã®æèã ã¨ãå¤ç¸æ§ããå¤ãå°è±¡ã§ãããã©ã¡ãããã³ã¨ããè¨èã§ã¯ããã¾ãããããã§ã¾ãã¯è¨èãå解ãã¦èªã¿è§£ãã¦ã¿ããã¨æãã¾ãã
ããªã¢ã¼ãã£ãºã (Polymorphism)ã¨ããè¨èã¯ããª(Poly)ã¨ã¢ã¼ãã£ãºã (Morphism)ã«ããããã¾ããããªã¯ãè¤æ°ã®ããã¢ã¼ãã£ãºã ã¯ãå½¢ç¶ãã¨ç½®ãæãããããã§ããã¤ã¾ãããªã¢ã¼ãã£ãºã ã¨ã¯ãè¤æ°ã®å½¢ç¶ãã¨èªã¿åãã¾ãã
ã¾ã æ½è±¡çã§æ´ã¿ã©ããããªãã§ãããå®éããªã¢ã¼ãã£ãºã ã¨ããè¨èèªä½ã¯çµæ§æ½è±¡çã§ãå¾ã»ã©ãç´¹ä»ãã¾ããçç©å¦ãªã©ã§ãç¨ããããå¹ åºãè¨èã®ããã§ããããã§ãããå°ãããã°ã©ãã³ã°çãªæå³ã«æ³¨ç®ãã¦ããããã¨æãã¾ããWikipediaã®åé ã®èª¬æãã¨ã¦ãããããããã§ãã
polymorphism is the provision of a single interface to entities of different types. Polymorphism (computer science) - Wikipedia
çãããã¨ãSingle interface for different typesãã¨ã§ãè¨ãã®ã§ãããããã¤ã¾ãããã°ã©ãã³ã°ã«ãããããªã¢ã¼ãã£ãºã ã¨ã¯ãè¤æ°ã®ç°ãªãåã«å¯¾ãã¦å ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãããã¨ã§ãã
3種é¡ã®ããªã¢ã¼ãã£ãºã
ããªã¢ã¼ãã£ãºã ã«ã¯å¤§ããåãã¦3種é¡ããã¾ããããããè¦ã¦ããã¾ããããªã¢ã¼ãã£ãã¯ãªé¨åã ããµã³ãã«ã軽ãæ¸ãã¦ããã¾ãã®ã§é°å²æ°ã ãæãã¦é ããã°ã¨æãã¾ãã
Ad hoc polymorphism
ããã¤ãã®éå®ãããåã«å¯¾ãã¦å ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæä¾ããããªã¢ã¼ãã£ãºã ã§ããå®è£ ä¸ã¯é¢æ°ã®ãªã¼ãã¼ãã¼ãã«ãã£ã¦å®ç¾ããã¾ããåãååã®é¢æ°ã«å¯¾ãã¦ãå¼æ°ãæ»ãå¤ã®åãå¤æ´ãããã®ãç¨æãã¾ãã
é¢æ°ãªã¼ãã¼ãã¼ããããªã¢ã¼ãã£ãºã ã§ããã¨ããã®ã¯å°ãæå¤ã«æããããæ¹ãããããã®ã§ã¯ãªãã§ããããããããããã確ãã«ãè¤æ°ã®ç°ãªãåã«å¯¾ãã¦å ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæä¾ããã¦ãã¾ãã
// Dogã¨Catã«å¯¾ãã¦touchã¨ããå ±éã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãã class Breeder { func touch(_ dog: Dog) { dog.say() } func touch(_ cat: Cat) { cat.say() } } let breeder = Breeder() breeder.touch(Dog()) // bowwow breeder.touch(Cat()) // meow
Parametric polymorphism
æ§ã ãªåã«é©ç¨ã§ãããããªã¤ã³ã¿ãã§ã¼ã¹ãæä¾ããããªã¢ã¼ãã£ãºã ã§ããå®è£ ä¸ã¯ããããGenericsãTemplateã¨å¼ã°ããè¨èªæ©è½ã«ããå®ç¾ããã¾ããåãå¼æ°ã¨ãã¦æ¸¡ããã¨ã§å¦çãæ§ã ãªåã«é©ç¨ãããã¨ãå¯è½ã«ãã¾ãã
é¢æ°åè¨èªã«ããã¦åã«ããªã¢ã¼ãã£ãºã ã¨è¨ãå ´åããã¡ããæãã¾ãã
// Animalã«é©åããããããåã«å¯¾ãã¦touchã¨ããå ±éã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãã func touch<A: Animal>(_ animal: A) { animal.say() } touch(Dog()) // bowwow touch(Cat()) // meow
Subtyping
å®è¡æã«ãµãã¯ã©ã¹ã®ãã¤ã³ã¿ããã¼ã¹ã¯ã©ã¹ã®ãã¤ã³ã¿ã¨ãã¦æ±ããã¨ã«ããããã¼ã¹ã¯ã©ã¹ã®å ±éãªã¤ã³ã¿ãã§ã¼ã¹ããåãµãã¯ã©ã¹ã®ç°ãªãå®è£ ãå¼ã¹ãããã«ããããªã¢ã¼ãã£ãºã ã§ãã
ãªãã¸ã§ã¯ãæåè¨èªã«ããã¦åã«ããªã¢ã¼ãã£ãºã ã¨ããå ´åããã¡ããæãã¾ãã
// Animalã¨Animalã®ãµãã¯ã©ã¹ã«touchã¨ããå ±éã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãã func touch(_ animal: Animal) { animal.say() } touch(Dog()) // bowwow touch(Cat()) // meow
ãªãããªã¢ã¼ãã£ãºã ãï¼
ãªããã®ãããªè¨èªæ©è½ãããã¨å¬ããã®ãï¼ã¨ãã話ã§ãããããã¯åã®å人çãªèããæ¸ããã¦ããã ãã¾ãã
ããªã¢ã¼ãã£ãºã ã¯ãè¤æ°ã®ç°ãªãåã«å¯¾ãã¦å ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãæä¾ãããã¨ãã§ããã¨ããã®ã¯åè¿°ãã¾ãããããã®æ¬è³ªã¯ãå ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãç¨æãããã¨ããé¨åã§ããã¨èãã¦ãã¾ãã
ãå ±éã®ã¤ã³ã¿ãã§ã¼ã¹ãç¨æãããã¨ããèãæ¹ã¯èº«ã®åãã®éå ·ã®ãã¶ã¤ã³ã«ãããµãã¦ãã¾ããPCãè¦ãã°USBãã¤ã¤ãã³ã¸ã£ãã¯ãªã©ãå ±éåãããã¤ã³ã¿ãã§ã¼ã¹ã°ããã§ãããããããããã¡ãªããã¯ãã¤ã³ã¿ãã§ã¼ã¹ã«é©åãã¦ããæ©å¨ã§ããã°ä½ã§ãåãä»ããããã¨ãããã¨ã§ããè¨ãæããã°ãå ±éã®ã¤ã³ã¿ãã§ã¼ã¹ã¯æè»ãªæ¡å¼µæ§ããããããã®ã§ããããããè¯ãã¤ã³ã¿ãã§ã¼ã¹ãã¶ã¤ã³ã§ããã°ãççµååããä¿é²ãã¾ãã
ã¾ããå ±éã®ã¤ã³ã¿ãã§ã¼ã¹ã«ã¯ãã¥ã¼ãã³ã¤ã³ã¿ãã§ã¼ã¹ä¸ã®ã¡ãªãããããã¾ãããã¿ã³ãã¹ã¤ãããªã©ã®ä½¿ãæ¹ãå ±éã§ããã°ä½¿ãæ¹ãæ°ãã«è¦ããå¿ è¦ãããã¾ãããããã¹ã®å²åãæ¸ããã¨ãæå¾ ã§ãã¾ãã
ããã°ã©ãã³ã°è¨èªã人éã使ãéå ·ãªã®ã§ãåæ§ã®ã¡ãªãããããã¨èãããã¾ããã¤ã¾ãæè»ãªæ¡å¼µæ§ã¨ããã¹ã®ä½æ¸ã§ããæ°ããæ©è½ã追å ããã¨ãã«ç¾ç¶ã®ã³ã¼ãã«ã»ã¨ãã©å¤æ´ãå ãããã¨ãªãèªç¶ãªå½¢ã§è¿½å ã§ãããï¼æè»ãªæ¡å¼µæ§ï¼ãifãswitchã§åå²ãããã¨ãªãå ±éã¤ã³ã¿ãã§ã¼ã¹ãå¼ã³åºãããã«ãããã¨ã§ãæ°ããªæ©è½ã追å ããã¨ãã«æ¸ãå ããç®æãæ¸ãããã¨ãã§ããããé£çµãå解ãæ¢ç´¢ãã½ã¼ããªã©æ±ç¨çãªå¦çã«å¯¾ãã¦å¼ã³åºãã¹ãé¢æ°ãããã«ããã£ããï¼ãã¹ã®ä½æ¸ï¼ãã¾ãã
ã¾ã¨ããã¨ããªãããªã¢ã¼ãã£ãºã ãå¬ããã®ãï¼ã«å¯¾ããçãã¯ãè¯ãã¤ã³ã¿ãã§ã¼ã¹ãè¨è¨ããã®ã«ä¾¿å©ã ãããã§ãããããåãèããããªã¢ã¼ãã£ãºã ã®ã¡ãªããã§ãã
ãã®ä»ã®åéã§ã®ããªã¢ã¼ãã£ãºã
ããªã¢ã¼ãã£ãºã ã¨ããè¨èãããæ·±ãç解ãããããä»åéã«ããã¦ã©ã®ããã«ä½¿ããã¦ããããããã£ã¨è¦ã¦ã¿ã¾ããã®ã§ãåèã¾ã§ã«ã©ãããçãåéãªã®ã§ééããªã©ãã£ããããã¾ããã
çç©å¦
åã種ã®çç©ã®ä¸ã§å½¢ãæ§é ãªã©ãç°ãªããã¨ãããªã¢ã¼ãã£ãºã ã¨è¨ãã¾ãã人éã§ããã°ç·å¥³ã®æ§å·®ãç®ã®è²ãèã®è²ãè¡æ¶²åãªã©ãæãããã®ãã¡éºä¼åã«åºã¥ããã®ãéºä¼çå¤å(Genetic polymorphism)ã¨ããã¾ãã
ç©è³ªç§å¦
åãåå¦çµæã ãè¤æ°ã®ç°ãªãçµæ¶å½¢ãã¨ããã¨ãããªã¢ã¼ãã£ãºã ã¨è¨ãã¾ãã温度ãå§åãªã©ãçµæ¶ãçæããã¨ãã®æ¡ä»¶ã«ãã£ã¦çµæ¶ç³»ã決ã¾ãããã§ããä¾ãã°ãã°ã©ãã¡ã¤ãã¨ãã¤ã¤ã¢ã³ãã¯ã©ã¡ããC(çç´ )ãããªãã¾ãã
ãããã«
ãããã§ããã§ãããããããªã¢ã¼ãã£ãºã ã«ã¤ãã¦æ§ã ãªè§åº¦ããè¦ã¦ããã¤ã¤ããã®æ¦å¿µãã¡ãªããã«ã¤ãã¦èå¯ãã¦ã¿ã¾ãããä½ãæ°ããªçºè¦ãããã°å¹¸ãã§ãã
åè
çµå±ã»ã¨ãã©è±èªã®Wikipediaãåèã«ãã¾ããã