â»ä¸è¨URLã®ãµã¤ããåèã«ãã¾ãããè±èªãã¾ãã¯æè¡çãªç¥èãè³ãããå
容ã«èª¤ããå«ã¾ããããããããã¾ãã®ã§ããäºæ¿ãã ããã
â»æ¬ã¨ã³ããªã®8å²ã¯åèãµã¤ãã®ç¿»è¨³ã§ããæ®ãã®2å²ã¯ç§ã®è§£éã§å çãå¤æ´ãæ½ããæ§æã§ãã
åèãµã¤ã http://confluence.jetbrains.net/display/Kotlin/Object+expressions+and+Declarations
æ°ãããµãã¯ã©ã¹ãæ示çã«å®£è¨ãããã¨ãªãã«ãã¯ã©ã¹ããããã«å¤æ´ãããªãã¸ã§ã¯ããçæããå¿ è¦ããããã¨ããæã ããã¾ããJavaã¯å¿åå é¨ã¯ã©ã¹ï¼anonymous inner classï¼ã§ãã®ã±ã¼ã¹ã対å¦ãã¾ããKotlinã¯ãªãã¸ã§ã¯ãå¼ã¨ãªãã¸ã§ã¯ã宣è¨ã§ãã®ã³ã³ã»ãããå°ãä¸è¬åãã¾ãã
ãªãã¸ã§ã¯ãå¼
ä½ãããã®åãç¶æ¿ããå¿åã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã¯æ¬¡ã®ããã«çæãã¾ãã
fun main(args : Array<String>) { val japanese = object : Greeter { override fun greet() = println("ããã«ã¡ã¯") } japanese.greet() //ãããã«ã¡ã¯ãã表示ããã } trait Greeter { fun greet() }
ã¹ã¼ãã¼ã¯ã©ã¹ãã³ã³ã¹ãã©ã¯ã¿ãæã¤å ´åã¯ãé©åãªã³ã³ã¹ãã©ã¯ã¿ã®ãã©ã¡ã¼ã¿ã渡ããªããã°ãªãã¾ãããå¤éç¶æ¿ã®å ´åã¯ãobject : ã«ç¶ãã¦ã«ã³ãã§åºåã£ã¦ã¹ã¼ãã¼ã¯ã©ã¹ãæå®ãã¾ãã
fun main(args : Array<String>) { val ab = object : A(1), B { override fun foo() { //ã»ã»ã» } } } open class A(val a : Int) trait B { fun foo() }
ä¸ä¸ãåãªããªãã¸ã§ã¯ãããå¿ è¦ãªå ´åã¯ã次ã®ããã«ç°¡åã«ããã¾ãã
val foo = object { var p : Int = 0 fun doSomething() { //ã»ã»ã» } }
ãªãã¸ã§ã¯ã宣è¨
ã·ã³ã°ã«ãã³ã¯é常ã«ä¾¿å©ãªãã¿ã¼ã³ã§ãããKotlinã§ã¯ç°¡åã«ã·ã³ã°ã«ãã³ã宣è¨ã§ãã¾ãã
object Singleton { fun hello() { println("Hello") } }
ããããªãã¸ã§ã¯ã宣è¨ï¼object declarationï¼ã¨å¼ã³ã¾ãããããå¤æ°ã«å²ãå½ã¦ããã¨ã¯ã§ãã¾ããããååã使ã£ã¦åç §ãããã¨ãã§ãã¾ããã¾ããã¹ã¼ãã¼ã¯ã©ã¹ãæã¤ãã¨ãã§ãã¾ãã
open class ABC() object Singleton : ABC() { fun hello() { println("Hello") } } fun main(args : Array<String>) { Singleton.hello() // Helloã表示ããã }
ãªãã¸ã§ã¯ãå¼ã¨ãªãã¸ã§ã¯ã宣è¨ã®ã»ãã³ãã£ã«ã«ãªéã
ãªãã¸ã§ã¯ãå¼ã¨ãªãã¸ã§ã¯ã宣è¨ã®éã«ã¯ã1ã¤éè¦ãªã»ãã³ãã£ã«ã«ãªéããããã¾ãã
- ãªãã¸ã§ã¯ã宣è¨ã¯ãæåã®ã¢ã¯ã»ã¹æã«åæåããã¾ãã
- ãªãã¸ã§ã¯ãå¼ã¯ã使ç¨ãããå ´æã§å³æã«å®è¡ãåæåããã¾ãã