ããªã¢ãŒãã£ãºã (Polymophism/å€çžæ§)ãšã¯
ããªã¢ãŒãã£ãºã ãšã¯, åèŠçŽ (å®æ°, å€æ°, åŒ, ãªããžã§ã¯ã, é¢æ°, ã¡ãœãããªã©) ã«ã€ããŠããããè€æ°ã®åã«å±ããããšãèš±ããšããæ§è³ª. Polymophism, å€çžæ§ãšããã.
åçš®ã®ã¯ã©ã¹ãã«ããŽãªã«åé¡ããŠãŸãšã, åºæ¬çãªåäœã»èšèšéšåãçµ±äžããããšã§, ãªããžã§ã¯ãã€ã³ã¹ã¿ã³ã¹ã®æ±ãã«æè»æ§ãšèŠåŸãæãããããã®æ©èœ.
å€çžå㯠ããã€ãåé¡ã§ãã.
- ã¢ãããã¯å€çž: é¢æ°ãé©çšããåãå¶éãã
- ãã©ã¡ãŒã¿å€çž: å ·äœçãªåã«ã€ããŠèšåããªãããšã«ãã, æ°ããåã«å¯Ÿå¿ã§ããããã«ãã
- ãµãã¿ã€ãå€çž:
ããšãã° Java ã ãš,
- ã¢ãããã¯å€çž: ãªãŒããŒããŒã
- ãã©ã¡ãŒã¿å€çž: ãžã§ããªã¯ã¹
- ãµãã¿ã€ãå€çž: ç¶æ¿
- refs:
Polymorphic type: å€çžå
ããŒã¿æ§é ã®ã³ã³ãã.
ããŒã¿åœ¢åŒã«äŸåããªãã³ã³ãã¥ãŒã¿ããã°ã©ãã³ã°æ¹åŒããžã§ããªã¯ã¹ããã°ã©ãã³ã°ãšãã.
ã¢ãããã¯å€çž
Clojureãã¢ãããã¯å€çžãæã€(multimethod).
å€éå®çŸ©/ãªãŒããŒããŒã(Overload)
ãªããžã§ã¯ãæåã«ãããã¢ãããã¯å€çžãšã¯, ãªãŒããŒããŒãã«çžåœãã.
ããã°ã©ãã³ã°èšèªã«ãããŠåäžã®ååïŒã·ã³ãã«ïŒãæã€é¢æ°ãããã¯ã¡ãœããããã³åäžã®æŒç®åèšå·ã«ã€ããŠè€æ°å®çŸ©ããå©çšæã«ããã°ã©ã ã®æèã«å¿ããŠéžæããããšã§è€æ°ã®åäœãè¡ãããä»çµã¿.
ç¶æ¿(inheritance) ãšäžŠãã§ããã°ã©ãã³ã°èšèªã«ãããŠããªã¢ãŒãã£ãºã ãå®çŸããããã®äžã€ã®æ段.
- ãªãŒããŒã©ã€ã(override) ãšãããžãééãããã. 泚æ.
- å€éå®çŸ© - Wikipedia, Multi-arity function
ð¡ãªãŒããŒããŒãã¯ã¢ãããã¯å€çžãªã®ããã©ã¡ããªãã¯å€çžãªã®ãåé¡
ãªããžã§ã¯ãæåã«ããããªãŒããŒããŒãã¯ã¢ãããã¯ãªã®ããã©ã¡ããªãã¯ãªã®ãã¯èŸŒã¿å ¥ã£ãŠããããã«èŠãã.
- ã¢ãããã¯å€çž: åäžã®ååããè€æ°ã®ç°ãªãé¢æ°ãæããŠããã
- ãã©ã¡ããªãã¯å€çž: åäžã®ååãæãïŒåäžã®ïŒé¢æ°ããè€æ°ã®ç°ãªãåã§äœ¿çšã§ãã
ããããé°å²æ°ã§ç解ããããšã«ãã.
ãã©ã¡ãŒã¿å€çž
ãã©ã¡ããªãã¯å€çžãšã.
åå€æ°
å€çžåã¯å®£èšãããã¯ã©ã¹, é¢æ°ã«å¯ŸããŠ, å©çšæã«å ·äœçãªåãäžãã. ãããåå€æ° (Type variable) ãšãã.
Java ã®ååã€ãã«ãŒã«ãããããã.
Generic Type: ç·ç§°å
åä»ããããããã°ã©ãã³ã°èšèªã«ãããŠããŒã¿åã®å®çŸ©ãšãããåç §ããåŒ (ååŒ) ã®äžéšã«ãã©ã¡ã¿ãèš±ãããšã«ãã£ãŠé¡äŒŒããæ§é ãæã€è€æ°ã®ããŒã¿åãäžæ¬ããŠå®çŸ©ããŠ, ããããéžæå©çšããä»çµã¿.
Subtyping: 掟çå
ããŒã¿å S ãä»ã®ããŒã¿å T ãš is-a é¢ä¿ã«ãããšã, S ã T ã®æŽŸçå (ã¯ãããã, subtype) ã§ãããšãã.
åºæ¬åã®ããŒã¿ãåŠçããããã«äœãããããã°ã©ã ã¯, ãã®æŽŸçåã®ããŒã¿ã§ãæ£ããåŠçããããšãã§ãã.
åºæ¬å-掟çåé¢ä¿ã§ã¯ãªã¹ã³ãã®çœ®æåå (Liskov Substitution Principle) ãæãç«ã€.
2 ã€ã®æ¹æ³ããã
- ã€ã³ã¿ãã§ãŒã¹: åãã°ã«ãŒãã§åé¡
- ç¶æ¿: åãéå±€æ§é ã§åé¡
inheritance: ç¶æ¿
ã»ãšãã©ã®ã¯ã©ã¹ããŒã¹ãªããžã§ã¯ãæåèšèªã§ã¯, ãµãã¯ã©ã¹ (ã€ã³ããªã¿ã³ã¹) ã掟çåã®æŠå¿µãå®çŸããŠãã.
ref: ç¶æ¿: Inheritance
interface: ã€ã³ã¿ãã§ãŒã¹
ref: ã€ã³ã¿ãã§ãŒã¹: Interface
åã¯ã©ã¹
Haskell ã®æŠå¿µ.
- åã¯å€ãã°ã«ãŒãåãã.
- åã¯ã©ã¹ã¯, åãã°ã«ãŒãåãã.
- ãã®çµæ, åã¯ã©ã¹ã®å¶çŽãä»ããé¢æ°ã¯, ãã®åã¯ã©ã¹ã®ã°ã«ãŒãã«å±ããŠããªãåã«ã¯é©çšã§ããªã.
- ã€ã³ã¹ã¿ã³ã¹åãšã¯åœè©²ã®åã¯ã©ã¹ã«æå±ãã宣èš.
ãã®èª¬æã¯ãããããã.
åãåé¡ããç¹ã§ããã°, Java ã®ã€ã³ã¿ãã§ãŒã¹ãšå矩.
OOP ãšFPã®æ¯èŒ
- ãªããžã§ã¯ãæåã¯ã¯ã©ã¹ã§ã€ã³ããªã¿ã³ã¹
- é¢æ°åã¯ä»£æ°çããŒã¿åã§éžæ
References
- ããªã¢ãŒãã£ãºã - Wikipedia
- åã·ã¹ãã å ¥é