shanonim
April 15, 2018
310

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

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

April 15, 2018

## Transcript

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

͜ͱΓΜ ͔Θ͍͍ ੩తܕ෇͚ ΦϒδΣΫτࢦ޲ ܕਪ࿦ ͔Θ͍͍ ॳ৺ऀͰ΋Ͱ͖Δʁ J04ΞϓϦ΋࡞ΕΔ +4τϥϯείϯύΠϧ ͔Θ͍͍ Φʔϓϯιʔε

+7.ݴޠ

11. ### w ܕਪ࿦ w ιʔείʔυ಺શͯͷม਺ͷํΛ໌ࣔతʹॻ͘ඞཁ͕ͳ͍ w จ຺͔Βม਺ͷํΛࣗಈతʹܾఆ͠ɺܕͷએݴΛলུ͢Δ val x = 1

ม਺Λ੔਺ܕͰॳظԽ͍ͯ͠Δˠ*OUܕͰ͋Δͱ൑அ͞ΕΔ val x = "Hello World!" ม਺ΛจࣈྻܕͰॳظԽ͍ͯ͠Δˠ4USJOHܕͰ͋Δͱ൑அ͞ΕΔ ಛ௃
12. ### w OVMM҆શʢOVMMڐ༰ܕͷαϙʔτʣ w /PNPSF/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ඇڐ༰ܕʹͳΔʣ ಛ௃

ಛ௃

16. ### ϦϦʔε w ৽͍͠ݴޠ w ೥݄ʹ,PUMJOϓϩδΣΫτΛΞφ΢ϯε ೥  w 1ZUIPO೥ ೥

 w 3VCZ೥ ೥  w 4DBMB೥ ೥  w ೥݄ʹਖ਼ࣜϦϦʔεʢਖ਼ࣜϦϦʔε͔Β೥ʣ
17. ### ։ൃ؀ڥ w 8FC w 5SZ,PUMJO w *%& w *OUFMMJ+*%&" w

&DMJQTF w "OESPJE4UVEJP "OESPJEΞϓϦ։ൃ༻
18. ### ׆༂ͷ෼໺ w .PCJMF w "OESPJE w ,PUMJO/BUJWF w IUUQTLPUMJOMBOHPSHEPDTSFGFSFODFOBUJWFPWFSWJFXIUNM w

8FC w ,PUMJOGPS+BWB4DSJQU w 4FSWFS4JEF w 4QSJOH'SBNFXPSL w IUUQTTQSJOHJPCMPHJOUSPEVDJOHLPUMJOTVQQPSUJOTQSJOHGSBNFXPSL
19. ### ֶशํ๏ w ॻ੶ w ,PUMJOೖ໳·Ͱͷॿ૸ಡຊ w ,PUMJOελʔτϒοΫ w ,PUMJOΠϯΞΫγϣϯ w

8FC w ,PUMJO,PBOT w IUUQTLPUMJOMBOHPSHEPDTUVUPSJBMTLPBOTIUNM w υοτΠϯετʔϧ w IUUQTEPUJOTUBMMDPNMFTTPOTCBTJD@LPUMJO
20. ### ίϛϡχςΟ w ೔ຊ,PUMJOϢʔβάϧʔϓ w IUUQLPUMJOMBOHKQIFSPLVBQQDPN w ษڧձ w ॳ৺ऀ޲͚ϋϯζΦϯ w

4XJGU,PUMJO߹ಉษڧձ w ΧϯϑΝϨϯε w ,PUMJOΧϯϑΝϨϯε Ծ ˠاըਐߦத
21. ### ·ͱΊ w ,PUMJO͸ɺ+FU#SBJOT͕։ൃͨ͠+7.ݴޠ w ʮ+BWBΑΓ΋҆શɾ؆ܿʹʯॻ͘͜ͱΛ໨తͱ͍ͯ͠Δ w ੩తܕ෇͚ΦϒδΣΫτࢦ޲ݴޠ w /VMM҆શܕਪ࿦ w

+BWBΤϯδχΞ΍ଞ+7.ݴޠܦݧऀ͕࢝Ί΍͍͢ w Φʔϓϯιʔε w .PCJMFͷଞʹ΋ɺ8FC4FSWFS4JEFͰ΋׆༂

25. ### ։ൃ؀ڥ w 5SZ,PUMJO w ϒϥ΢βͰ࢖͑Δ։ൃ؀ڥ w *OUFMMJ+*%&" ,PUMJO1MVHJO w +FU#SBJOT͕ఏڙ͢Δ*%&ʢ౷߹։ൃ؀ڥʣ

w ,PUMJO1MVHJO͸ϓϦΠϯετʔϧࡁΈ

World!") }

36. ### fun main(args: Array<String>) { println("Hello World!") } w AGVOAͰؔ਺Λఆٛ w

ؔ਺͸ϑΝΠϧͷτοϓϨϕϧʹએݴͰ͖Δ w Ϋϥεͷதʹஔ͘ඞཁ͕ͳ͍

38. ### fun main(args: Array<String>) { println("Hello World!") } w 4ZTUFNPVUQSJOUMOͷ୅ΘΓʹQSJOUMO w

,PUMJOͷඪ४ϥΠϒϥϦ͸ɺ+BWBͷඪ४ϥΠϒϥϦͷ ϥούʔΛଟ਺ఏڙ͍ͯ͠Δ

40. ### ·ͱΊ w *OUFMMJ+*%&"Ͱ,PUMJO։ൃ w )FMMP8PSME͔ΒֶͿ,PUMJOͷಛ௃ w ؔ਺ͷએݴʹ͸AGVOAΛ࢖͏ w ؔ਺͸ϑΝΠϧͷτοϓϨϕϧʹએݴͰ͖Δ w

Ҿ਺ม਺ͷܕ͸໊લͷޙΖͰࢦఆ͢Δ w ηϛίϩϯແ͠

43. ### ม਺ val name = "shinohara" val age = 27 w

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

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

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

Array<String>) { println(keyword) keyword = "Good afternoon!" println(keyword) }

48. ### ·ͱΊ w ,PUMJOͰ͸ɺม਺ͷܕએݴΛলུͰ͖Δʢܕਪ࿦ʣ w ໌ࣔతʹॻ͘͜ͱ΋Մೳ w ม਺ͷछྨ w Πϛϡʔλϒϧʢෆมͳʣม਺ˠWBM w

ϛϡʔλϒϧʢมߋՄೳͳʣม਺ˠWBS

50. ### ഑ྻ w ഑ྻ͸Ϋϥε"SSBZʹΑͬͯදݱ͞ΕΔ w ඪ४ϥΠϒϥϦؔ਺BSSBZ0GΛ࢖ͬͯΈΔ fun main(args: Array<String>) { val

colors = arrayOf("red", "green", "blue") println(colors[0]) }
51. ### ίϨΫγϣϯ w ஋ͷίϯςφͱͳΔΦϒδΣΫτΛʮίϨΫγϣϯʯͱݺͿ w ϦετηοτϚοϓ w ͜͜Ͱ͸ɺϦετΛྫʹڍ͛ͯઆ໌ w ਺஋ͷϦετΦϒδΣΫτΛఆٛͯ͠αΠζͱ൪໨ͷཁૉΛग़ྗͯ͠ΈΔ fun

main(args: Array<String>) { val ints: List<Int> = listOf<Int>(1, 2, 3) println(ints.size) println(ints[0]) }

53. ### ίϨΫγϣϯ 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͸Ϧετͷ஋௥Ճ࡟আ͕Ͱ͖Δ

55. ### JGFMTF w είΞ͕఺Ҏ্ͩͬͨΒTVDDFTTɺ఺ະຬͩͬͨΒGBJMVSFͱ QSJOUͯ͠ΈΔ fun main(args: Array<String>) { val score

= 80 if (score >= 60) { println("success") } else { println("failure") } }
56. ### JGFMTF w ,PUMJOͷJG͸ࣜͰ͋ΓɺจͰ͸ͳ͍ w จͱࣜͷҧ͍ w ࣜ͸ɺผͷࣜͷҰ෦ͱͯ͠࢖༻Ͱ͖Δ஋Λ࣋ͭ w จ͸ɺϒϩοΫͰғ·ΕͨτοϓϨϕϧͷཁૉͰ͋Γɺ ͦΕࣗ਎͸஋Λ࣋ͨͳ͍

fun main(args: Array<String>) { val score = 80 val message = if (score >= 60) "success" else "failure" println(message) }
57. ### 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.") } }
58. ### XIFO w ෳࡶͳJGFMTFͷՄಡੑ޲্ʹ༗ޮ 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 -> { ... } } }

60. ### GPS w Ϧετͷத਎Λ͢΂ͯQSJOUͯ͠ΈΔ fun main(args: Array<String>) { val colors =

listOf("red", "green", "blue") for (color in colors) { println(color) } } ܁Γฦ͠ՄೳͳΦϒδΣΫτͷ ཁૉΛॱ൪ʹऔΓग़͢
61. ### 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) }
62. ### ·ͱΊ w ༷ʑͳΦϒδΣΫτ w ഑ྻ w ϦετηοτϚοϓ w ϛϡʔλϒϧΠϛϡʔλϒϧΛ࢖͍෼͚Δ w

৚݅෼ذ w ,PUMJOͷJG͸ࣜ w ෳࡶͳJGFMTF͸XIFOΛ׆༻ͯ͠ΈΔ w ܁Γฦ͠ w GPSΛ࢖ͬͯ܁Γฦ͠ՄೳͳΦϒδΣΫτͷཁૉʹॱ൪ʹΞΫηεͰ͖Δ w Ϩϯδ YZ Ͱൣғࢦఆͨ͠ΦϒδΣΫτΛੜ੒ͯ͠ॱ൪ʹΞΫηεͰ͖Δ

65. ### 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[[
66. ### '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) } } }

+ b }
69. ### ؔ਺ fun sum(a: Int, b: Int): Int { return a

+ b } ؔ਺໊ Ҿ਺ ໭Γ஋ͷܕ ؔ਺ͷຊମ
70. ### ؔ਺ w ଍͠ࢉͷ݁Ռ͕ग़ྗ͞ΕΔϓϩάϥϜΛ࡞ͬͯΈΔ fun main(args: Array<String>) { println(sum(1, 2)) }

fun sum(a: Int, b: Int): Int { return a + b }

72. ### w ʮTRVBSFʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺Λ৐ͯ͠ฦ͢ w ʮNBYʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨͭͷ੔਺ͷ͏ͪɺେ͖͍ํΛฦ͢ w ʮJT&WFOʯ

w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺͕ۮ਺ͷ৔߹͸USVFΛɺح਺ͷ৔߹͸GBMTFΛฦ͢ w ʮGBDUPSJDBMʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨ੔਺ͷ֊৐Λฦ͢ w ྫ ͷ֊৐    w ʮJT\$POUBJOT5ISFFʯ w Ҿ਺ͱͯ͠༩͑ΒΕͨϦετʹʮʯؚ͕·ΕΔ͔Ͳ͏͔Λ#PPMFBOͰฦ͢ ͍Ζ͍Ζͳؔ਺Λ࡞ͬͯΈΔ

74. ### Ϋϥε class Person(val name: String) val person: Person = Person("shinohara")

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

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

fun main(args: Array<String>) { println(person.name) // shinohara } ϓϩύςΟ ϓϩύςΟ
77. ### Ϋϥεͱϝιου 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 }
78. ### 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ʯ
79. ### 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ʯ