Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin初心者向けハンズオン #3 / Kotlin Hands-on for Begin...

shanonim
April 15, 2018

Kotlin初心者向けハンズオン #3 / Kotlin Hands-on for Beginners

2018.04.15
Kotlin初心者向けハンズオン #3
https://kotlin.connpass.com/event/79344/

shanonim

April 15, 2018
Tweet

More Decks by shanonim

Other Decks in Technology

Transcript

  1. ྲྀߦͬͯΔײ +7. "OESPJE OVMM҆શ ࠷ۙΑ͘ฉ͘ #FUUFS+BWB ৽͍͠ݴޠ αʔόʔαΠυ 4QSJOH'SBNFXPSL ໊લ͕͔Θ͍͍

    ͜ͱΓΜ ͔Θ͍͍ ੩తܕ෇͚ ΦϒδΣΫτࢦ޲ ܕਪ࿦ ͔Θ͍͍ ॳ৺ऀͰ΋Ͱ͖Δʁ J04ΞϓϦ΋࡞ΕΔ +4τϥϯείϯύΠϧ ͔Θ͍͍ Φʔϓϯιʔε
  2. w ܕਪ࿦ w ιʔείʔυ಺શͯͷม਺ͷํΛ໌ࣔతʹॻ͘ඞཁ͕ͳ͍ w จ຺͔Βม਺ͷํΛࣗಈతʹܾఆ͠ɺܕͷએݴΛলུ͢Δ val x = 1

    ม਺Λ੔਺ܕͰॳظԽ͍ͯ͠Δˠ*OUܕͰ͋Δͱ൑அ͞ΕΔ val x = "Hello World!" ม਺ΛจࣈྻܕͰॳظԽ͍ͯ͠Δˠ4USJOHܕͰ͋Δͱ൑அ͞ΕΔ ಛ௃
  3. w OVMM҆શʢOVMMڐ༰ܕͷαϙʔτʣ w /PNPSF/VMM1PJOUFS&YDFQUJPO w OVMMڐ༰ܕΛαϙʔτ͢Δ͜ͱͰɺ/1&ΤϥʔΛ࣮ߦ࣌Τϥʔ͔Βɹɹɹ ίϯύΠϧ࣌Τϥʔʹͯ͠ղܾ /* Java */

    int strLen(String s) { return s.length(); } /* Kotlin */ fun strLen(s: String) = s.length OVMMΛҾ਺ͱͯ͠ݺͼग़͢ͱ/1& OVMMΛҾ਺ͱͯ͠ݺͼग़͢͜ͱ͕ڐՄ͞Εͳ͍ ʢίϯύΠϧΤϥʔʣ /* Kotlin */ fun strLen(s: String?) = ... Ҿ਺ʹOVMM͕ೖΔ͜ͱΛڐՄ͢ΔʢOVMMBCMFʣͷ৔߹͸ ܕʹٙ໰ූΛ෇͚ͯ໌ࣔ͢Δ ʢʹ໌ࣔ͠ͳ͚Ε͹ɺશͯͷܕ͸OVMMඇڐ༰ܕʹͳΔʣ ಛ௃
  4. ϦϦʔε w ৽͍͠ݴޠ w ೥݄ʹ,PUMJOϓϩδΣΫτΛΞφ΢ϯε ೥  w 1ZUIPO೥ ೥

     w 3VCZ೥ ೥  w 4DBMB೥ ೥  w ೥݄ʹਖ਼ࣜϦϦʔεʢਖ਼ࣜϦϦʔε͔Β೥ʣ
  5. ։ൃ؀ڥ w 8FC w 5SZ,PUMJO w *%& w *OUFMMJ+*%&" w

    &DMJQTF w "OESPJE4UVEJP "OESPJEΞϓϦ։ൃ༻
  6. ׆༂ͷ෼໺ w .PCJMF w "OESPJE w ,PUMJO/BUJWF w IUUQTLPUMJOMBOHPSHEPDTSFGFSFODFOBUJWFPWFSWJFXIUNM w

    8FC w ,PUMJOGPS+BWB4DSJQU w 4FSWFS4JEF w 4QSJOH'SBNFXPSL w IUUQTTQSJOHJPCMPHJOUSPEVDJOHLPUMJOTVQQPSUJOTQSJOHGSBNFXPSL
  7. ֶशํ๏ w ॻ੶ w ,PUMJOೖ໳·Ͱͷॿ૸ಡຊ w ,PUMJOελʔτϒοΫ w ,PUMJOΠϯΞΫγϣϯ w

    8FC w ,PUMJO,PBOT w IUUQTLPUMJOMBOHPSHEPDTUVUPSJBMTLPBOTIUNM w υοτΠϯετʔϧ w IUUQTEPUJOTUBMMDPNMFTTPOTCBTJD@LPUMJO
  8. ίϛϡχςΟ w ೔ຊ,PUMJOϢʔβάϧʔϓ w IUUQLPUMJOMBOHKQIFSPLVBQQDPN w ษڧձ w ॳ৺ऀ޲͚ϋϯζΦϯ w

    4XJGU,PUMJO߹ಉษڧձ w ΧϯϑΝϨϯε w ,PUMJOΧϯϑΝϨϯε Ծ ˠاըਐߦத
  9. fun main(args: Array<String>) { println("Hello World!") } w AGVOAͰؔ਺Λఆٛ w

    ؔ਺͸ϑΝΠϧͷτοϓϨϕϧʹએݴͰ͖Δ w Ϋϥεͷதʹஔ͘ඞཁ͕ͳ͍
  10. fun main(args: Array<String>) { println("Hello World!") } w 4ZTUFNPVUQSJOUMOͷ୅ΘΓʹQSJOUMO w

    ,PUMJOͷඪ४ϥΠϒϥϦ͸ɺ+BWBͷඪ४ϥΠϒϥϦͷ ϥούʔΛଟ਺ఏڙ͍ͯ͠Δ
  11. ม਺ val name = "shinohara" val age = 27 w

    +BWBͷม਺એݴͩͱɺ࢝ΊʹܕΛॻ͘ͱ͜Ζ͔Β࢝·Δ w ,PUMJOͰ͸ଟ͘ͷม਺એݴͰܕΛলུͰ͖Δ val name: String = "shinohara" val age: Int = 27 w ඞཁͰ͋Ε͹ɺ໌ࣔతʹܕΛࢦఆ͢Δ͜ͱ΋Ͱ͖Δ
  12. ม਺ w Πϛϡʔλϒϧʢෆมͳʣม਺ w WBM WBMVFʹ༝དྷ  w WBMͰએݴ͞Εͨม਺͸ɺҰ౓ॳظԽ͞ΕΔͱ࠶౓஋ΛׂΓ ౰ͯΔ͜ͱ͸Ͱ͖ͳ͍

    w +BWBʹ͓͚ΔpOBMม਺ w ϛϡʔλϒϧʢมߋՄೳͳʣม਺ w WBS WBSJBCMFʹ༝དྷ  w ஋ΛมߋͰ͖Δ w +BWBʹ͓͚ΔpOBMͰͳ͍௨ৗͷม਺
  13. ม਺ val age = 27 age = 28 var age

    = 27 age = 28 WBM͸มߋͰ͖ͳ͍ WBS΁ͷ୅ೖ͸Մೳ
  14. ม਺ w ϛϡʔλϒϧͳม਺Λએݴͯ͠ɺ஋Λ୅ೖͯ͠ΈΔ var keyword = "Good morning!" fun main(args:

    Array<String>) { println(keyword) keyword = "Good afternoon!" println(keyword) }
  15. ίϨΫγϣϯ fun main(args: Array<String>) { val ints: MutableList<Int> = mutableListOf<Int>(1,

    2, 3) println(ints[0]) ints[0] = 4 println(ints[0]) } fun main(args: Array<String>) { val ints: MutableList<Int> = mutableListOf<Int>(1, 2, 3) println(ints) ints.add(4) println(ints) } w .VUBCMF-JTU͸Ϧετͷ஋௥Ճ࡟আ͕Ͱ͖Δ
  16. XIFO w +BWBʹ͓͚ΔTXJUDIจΛڧྗʹͨࣜ͠ w ෼ذ৚݅ʹఆ਺Ҏ֎ͷࢦఆ͕Ͱ͖Δ w ʮOVNCFS͕ͷͱ͖ʯͷ৚݅෼ذΛXIFOͰ࡞ͬͯΈΔ fun main(args: Array<String>)

    { val number = 1 when (number) { 1 -> println("number is one.") 2, 3 -> println("number is two or three.") else -> println("unknown.") } }
  17. XIFO w ෳࡶͳJGFMTFͷՄಡੑ޲্ʹ༗ޮ fun main(args: Array<String>) { val number =

    1 if (number == 1) { } else if (number == 2) { } else if (number == 3) { } else if (number == 4) { } else if (number == 5) { } } fun main(args: Array<String>) { val number = 1 when (number) { 1 -> { ... } 2 -> { ... } 3 -> { ... } 4 -> { ... } 5 -> { ... } } }
  18. GPS w Ϧετͷத਎Λ͢΂ͯQSJOUͯ͠ΈΔ fun main(args: Array<String>) { val colors =

    listOf("red", "green", "blue") for (color in colors) { println(color) } } ܁Γฦ͠ՄೳͳΦϒδΣΫτͷ ཁૉΛॱ൪ʹऔΓग़͢
  19. GPS Ϩϯδ w Ϩϯδ w ൣғΛද͢ΦϒδΣΫτ w ʮdʯΛද͢ΦϒδΣΫτ͸ʮʯͱදݱͰ͖Δ w GPSͱϨϯδΛ૊Έ߹Θͤͯɺ೚ҙͷൣғʹରͯ͠ॱ൪ʹΞΫηεͰ͖Δ

    w ͔Β·Ͱͷ਺Λ͢΂ͯ଍ͯ͠QSJOUͯ͠ΈΔ fun main(args: Array<String>) { var sum = 0 for (i in 1..10) { sum += i } println(sum) }
  20. ·ͱΊ w ༷ʑͳΦϒδΣΫτ w ഑ྻ w ϦετηοτϚοϓ w ϛϡʔλϒϧΠϛϡʔλϒϧΛ࢖͍෼͚Δ w

    ৚݅෼ذ w ,PUMJOͷJG͸ࣜ w ෳࡶͳJGFMTF͸XIFOΛ׆༻ͯ͠ΈΔ w ܁Γฦ͠ w GPSΛ࢖ͬͯ܁Γฦ͠ՄೳͳΦϒδΣΫτͷཁૉʹॱ൪ʹΞΫηεͰ͖Δ w Ϩϯδ YZ Ͱൣғࢦఆͨ͠ΦϒδΣΫτΛੜ੒ͯ͠ॱ൪ʹΞΫηεͰ͖Δ
  21. fun main(args: Array<String>) { for (i in 1..100) { when

    { i % 15 == 0 -> println("FizzBuzz") i % 3 == 0 -> println("Fizz") i % 5 == 0 -> println("Buzz") else -> println(i) } } } 'J[[#V[[
  22. 'J[[#V[[ fun main(args: Array<String>) { (1..100).forEach { i -> when

    { i % 15 == 0 -> println("FizzBuzz") i % 3 == 0 -> println("Fizz") i % 5 == 0 -> println("Buzz") else -> println(i) } } }
  23. ؔ਺ fun sum(a: Int, b: Int): Int { return a

    + b } ؔ਺໊ Ҿ਺ ໭Γ஋ͷܕ ؔ਺ͷຊମ
  24. w ʮTRVBSFʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺Λ৐ͯ͠ฦ͢ w ʮNBYʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨͭͷ੔਺ͷ͏ͪɺେ͖͍ํΛฦ͢ w ʮJT&WFOʯ

    w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺͕ۮ਺ͷ৔߹͸USVFΛɺح਺ͷ৔߹͸GBMTFΛฦ͢ w ʮGBDUPSJDBMʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺ͷ֊৐Λฦ͢ w ྫ ͷ֊৐    w ʮJT$POUBJOT5ISFFʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨϦετʹʮʯؚ͕·ΕΔ͔Ͳ͏͔Λ#PPMFBOͰฦ͢ ͍Ζ͍Ζͳؔ਺Λ࡞ͬͯΈΔ
  25. Ϋϥε class Person(val name: String) val person: Person = Person("shinohara")

    fun main(args: Array<String>) { println(person.name) // shinohara } Ϋϥεఆٛ Πϯελϯεੜ੒
  26. Ϋϥε class Person(val name: String) val person: Person = Person("shinohara")

    fun main(args: Array<String>) { println(person.name) // shinohara } ϓϥΠϚϦίϯετϥΫλͱҾ਺
  27. Ϋϥε class Person(val name: String) val person: Person = Person("shinohara")

    fun main(args: Array<String>) { println(person.name) // shinohara } ϓϩύςΟ ϓϩύςΟ
  28. Ϋϥεͱϝιου class Person(val name: String) { fun introduceMyself() { println("I

    am ${name}”) } } val person: Person = Person("shinohara") fun main(args: Array<String>) { person.introduceMyself() // I am shinohara }
  29. w Ϋϥε໊ʮ*OU1BJSʯ w ͭͷϓϩύςΟΛ࣋ͭ w pSTU*OU w TFDPOE*OU w ͭͷϝιουΛ࣋ͭ

    w TVN  w pSTUͱTFDPOEͷ࿨Λฦ͢ w NBY  w pSTUͱTFDPOEͷ͏ͪେ͖͍ํΛฦ͢ w TXBQ  w pSTUͱTFDPOE͕ٯʹͳͬͨ*OU1BJSΦϒδΣΫτΛฦ͢ Ϋϥεʮ*OU1BJSʯ
  30. class IntPair(val first: Int, val second: Int) { fun sum()

    = first + second fun max() = if (first > second) first else second fun swap() = IntPair(second, first) } fun main(args: Array<String>) { val intPair = IntPair(2, 4) println("sum: " + intPair.sum()) println("max: " + intPair.max()) println("swapped object (first): " + intPair.swap().first) println("swapped object (second): " + intPair.swap().second) } Ϋϥεʮ*OU1BJSʯ