Scalaã§ã®ãªãã¸ã§ã¯ãæåãµãã¼ã
ããä»ããã¯ãããScalaãã¯ä»åã§æçµåã¨ãªãã¾ããä»åã¯ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ãScalaãã©ã®ããã«ãµãã¼ããã¦ãããã解説ãã¾ãã
ãå¤ãã®ããã°ã©ãã³ã°è¨èªã¨åæ§ã«ãScalaã§ã¯ã¯ã©ã¹ãå®ç¾©ããç¶æ¿é¢ä¿ãæããããã¨ãå¯è½ã§ããScalaã®ä¸çã§ã¯ã1ï¼Intåï¼ãtrueï¼Booleanåï¼ãªã©Javaã§ã¯ããªããã£ãåã¨ãã¦åãæ±ããã¦ããå¤ã«ã¤ãã¦ãããªãã¸ã§ã¯ãã«ãªã£ã¦ãããã¡ã½ãããå¼ã³åºããã¨ãªã©ãè¡ãã¾ãï¼Rubyã§ãåãã§ããï¼ãåå解説ããé¢æ°ããªãã¸ã§ã¯ãã§ãã
ãããã«ãåç·¨ã§ç°¡åã«ç´¹ä»ããã±ã¼ã¹ã¯ã©ã¹ããã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ããçæããæ§æãMix-Inãå¯è½ã¨ãããã¬ã¤ããªã©ããã¾ãã¾ãªããã°ã©ãã³ã°è¨èªã®ç¹å¾´ãåãå ¥ãããã¤ããªãããªãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ããµãã¼ããã¦ããã®ã§ãã
ã¯ã©ã¹ã«ã¤ãã¦ã®ãããã
ããä»ããã§ãé ããªã ããããå§ããScala åç·¨ãã§è§£èª¬ããå 容ã§ãããScalaã§ã®ã¯ã©ã¹å®ç¾©ã®æ¹æ³ããããããã¾ãã
ãScalaã§ã¯ãã¯ã©ã¹ã®å®£è¨ã¯classãã¼ã¯ã¼ããç¨ãã¾ãã次ã®ãããªæ¸å¼ã¨ãªãã¾ãã
<case|abstract|sealed> class ã¯ã©ã¹å[åãã©ã¡ã¼ã¿]( <val|var>å¼æ°å:å¼æ°å,・・・ ) extends ã¹ã¼ãã¼ã¯ã©ã¹ã¾ãã¯ãã¬ã¤ã with ãã¬ã¤ã1 with ãã¬ã¤ã2 ... { ã¯ã©ã¹æ¬ä½ } }
ããã¬ã¤ãã«ã¤ãã¦ã¯å¾ã»ã©èª¬æãã¾ãã ã§ã¯ãããã¤ãå ·ä½çãªã¯ã©ã¹å®ç¾©ã®ä¾ãæãã¾ãã
// æ½è±¡ã¯ã©ã¹ æ¥½å¨ abstract class Instrument { def play:Unit // ãµãã¯ã©ã¹ã§å®è£ ããæ½è±¡ã¡ã½ãã } // ã®ã¿ã¼ ã³ã³ã¹ãã©ã¯ã¿ã«ååãã¨ã class Guitar( val name:String ) extends Instrument { def play:Unit = println("ã®ã£ã¼ã¼ã³ãã!!") // æ½è±¡ã¡ã½ãããå®è£ } // ãã¼ã¹ ã³ã³ã¹ãã©ã¯ã¿ã«ååãã¨ã class Base( val name:String ) extends Instrument { def play:Unit = println("ããã³ãã³") // æ½è±¡ã¡ã½ãããå®è£ }
ãæ½è±¡ã¯ã©ã¹Instrumentãç¶æ¿ããå½¢ã§å ·è±¡ã¯ã©ã¹Guitarã¨Baseãå®ç¾©ããã¦ãã¾ããInstrumentã¯ã©ã¹ã¯æ½è±¡ã¡ã½ããplayãæã£ã¦ãããããã¯ãµãã¯ã©ã¹ã§å®è£ ãå¿ è¦ã§ãã
ãGuitarã¯ã©ã¹ãBaseã¯ã©ã¹ã¯ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«Stringåã®nameãåãã¾ããããã¯ãvalãä»ä¸ããã¦ããã®ã§å¼æ°ã§åãåã£ãå¤ã¯publicãªãã£ã¼ã«ãã¨ãã¦å ¬éããã¾ããvalã§ãã®ã§ãèªã¿åãå°ç¨ãã£ã¼ã«ãã«ãªãã¾ãããvarãæå®ããå ´åã¯æ¸ãè¾¼ã¿ãå¯è½ãªãã£ã¼ã«ãã«ãªãã¾ãã
ãREPLã§åä½ç¢ºèªãã¦ã¿ã¾ãããã
scala> val guiter = new Guiter( "Fender Jazz Master '65") guiter: Guiter = Guiter@54477b4e scala> guiter.play ã®ã£ã¼ã¼ã³ãã!! scala> guiter.name res1: String = Fender Jazz Master '65
ãããã¾ã§ããåç·¨ã§åãä¸ããã¯ã©ã¹ã«é¢ãã¦ã®ããããã§ãã