ä»åã¯Kotlinã®åºæ¬çãªè¶ å ¥éã®å 容ããŸãšããŠãããŸãã
#ããã°ã©ã ã®3ã€ã®åºæ¬æ§é
Kotlinã®è§£èª¬ã®åã«ãããã°ã©ã ã®åºæ¬æ§é ã«ã€ããŠè©±ããŸãã
1,é 次é²è¡
ãšã¯ãããã°ã©ã ãæžãããŠããäžããé ã«åŠçãããŠãããšããããã°ã©ã ã®æ§é ã§ãã
2,æ¡ä»¶åå²
ãšã¯ãç¹å®ã®æ¡ä»¶ã®ãšãã¯AãšããåŠçããããããªããšãã¯BãšããåŠçãããããã°ã©ã æ§é ã§ãã
3,ç¹°ãè¿ã
ãšã¯ã決ãŸã£ãåæ°ãæ¡ä»¶ãæºãããŸã§åãåŠçãç¹°ãè¿ãããã°ã©ã æ§é ã§ãã
ç¹°ãè¿ãã¯ãå埩åŠçãšãã£ãããããŸãã
#Kotlinã®ç¹åŸŽ
Kotlinã¯ãã¢ãã³ãªèšèª
ãã³ã³ãã€ã©èšèª
ããªããžã§ã¯ãæåèšèª
ãšãã£ãç¹åŸŽããããŸãã
ã¢ãã³ãªèšèªãšã¯ãçŸä»£çãªããã°ã©ãã³ã°èšèªã®æ§æãåãå
¥ããŠããŠãèªã¿ããããŠæžããããããã°ã©ãã³ã°èšèªã®ããšããããŸãã
ã³ã³ãã€ã©èšèªãšã¯ãæ©æ¢°èªã«äžæ¬ããŠå€æããŠããå®è¡ããããã°ã©ãã³ã°èšèªã®ããšã§ãåŠçãé«éãšããç¹åŸŽããããŸãã
ãªããžã§ã¯ãæåèšèªãšã¯ããªããžã§ã¯ãæåéçºãšãããããŒã¿ãšåŠçãã1ã»ãããšããŠããã°ã©ã ãçµã¿ç«ãŠãŠããéçºææ³ã«é©ããèšèªã®ããšã§ãã
#é¢æ°ã«ã€ããŠ
ãŸãåãã«é¢æ°ã«ã€ããŠèª¬æããŠãããŸãã
é¢æ°ã®äŸ¿å©ãªãšããã¯ã
åãã³ãŒããäœåºŠãæžããªããŠæžã
åããã®ãä»ã®å Žé¢ã§äœ¿ãã
äžåºŠå®çŸ©ããã°ãä»ã®äººã䜿ãã
é¢æ°ã«ã¯ãèªåã§äœãé¢æ°ã
ãšãKotlinãçšæããŠãããé¢æ°
ã®ïŒçš®é¡ãããã
ïŒçªç®ã®KotlinãçšæããŠãããé¢æ°
ã¯ããããããå®çŸ©ãããŠãããŠããŸãã
èªåã®å Žåã¯ãããããAndroidã®ã¢ããªéçºãè¡ãªã£ãŠããäºå®ãªã®ã§ãå
¬åŒã®ããã¥ã¡ã³ãã«äœ¿ãæ¹ãæžããŠãããŸãã
ããã°ã©ã ã®å§ç¹ã®ããšããšã³ããªãã€ã³ã
ãšèšããŸãã
kotlinã§ã¯ãmainé¢æ°ããåŠçãå§ãŸããŸãã
èªåã§äœãé¢æ°ã
èªåã§äœãé¢æ°ã«ã¯äžèšã®ãããªæ±ºãŸã£ãæ§æããããŸãã
åŒæ°åãèšå
¥ããã¡ãªãããšããŠã¯ãåŠçã®äžã§äœ¿ãã
ãé¢æ°ã«åŒæ°ãå
¥ããããšã§ãåŠçã®ã§ããå¹
ãåºãã
ã[ãªãŒããŒã©ã€ã]ããéã«ã³ã³ãã€ã©ãå€æã§ãã
ããšãªã©ãæããããŸãã
â»ãªãŒããŒã©ã€ã
ãšã¯ãäžèšã§èšããšãã¡ãœããã®äžæžãã®ããšã§ãã
æ§æ
fun é¢æ°å(åŒæ°å: åŒæ°å)ã{
å®è¡ããåŠç
return ã§æ»ãå€ãè¿ã
}
以äžã®ãµã³ãã«ã³ãŒãã§ã¯ã3ã€ã®æ°åã®åãåãã¡ãœãããäœãã3ã€ã®æ°åã®å¹³åã衚瀺ãããŠããã
returnã䜿ãããšã§ãèšç®ããçµæãããã«èšç®ããããæååãšãã£ã€ãããããããšãå¯èœãšãªããŸãã
fun main() {
val result = avg(10, 3, 5)
println(result)
}
fun avg(i1 : Int, i2: Int,i3: Int) :Int{
return (i1 + i2 + i3) / 3
}
#Kotlinã®ãœãŒã¹ã³ãŒãã®åºæ¬èšè¿°
Kotlinã§ã¯ãé¢æ°ããããã°ã©ã ãã¹ã¿ãŒãããŸãã
é¢æ°ãäœãããšãé¢æ°ãå®çŸ©ãããšãããŸãã
ããã°ã©ãã³ã°ã§ã®é¢æ°ã¯ãåŠçã®ããšã§ãã
kotlinã§ã¯ãmainé¢æ°
ããããã°ã©ã ãå§ãŸããŸãã
æåã衚瀺ããé¢æ°println()
ã¡ãœããã䜿çšããŠãæååããæ°åã衚瀺ããã
æååã®å Žåã¯ãã""ãã§å²ããæ°åã®å Žåã¯å¿
èŠãªãã
å®è¡ãããšã
ãGood morningã
ãããã«ã¡ã¯ã
ã1ã
ã衚瀺ãããããã«ããã
fun main() {
println("Good morning")
println("ããã«ã¡ã¯")
println(1)
}
#å€æ°ã«ã€ããŠ
å€æ°ã¯äŸãããªãç®±ã®ããšã§ãã
å€æ°ãããããšã§ãæåãæ°åãªã©ã®ããŒã¿ãå€æ°ã«å
¥ããŠããããšãã§ããŸãã
å€æ°ã«ããŒã¿ãå
¥ããããšãã代å
¥ã
ãšãããåãåºãããšããåç
§ã
ãšãããŸãã
###å€æ°ã«ã¯ååãã€ããããšãã§ããå€æ°åãšãããŸãã
å€æ°åã«äœ¿ããæåã«ã¯ãã«ãŒã«ããããŸãã
âå€æ°åã«ã¯ãã¢ã«ãã¡ããããæ°åãã¢ã³ããŒã¹ã³ã¢( _ )ã䜿ããŸãã
âäºçŽèª
ã¯å€æ°åã«ããããšãã§ããŸããã
äºçŽèª
ãšã¯ããreturnããclassããfor ããwhileããªã©ãããã°ã©ãã³ã°èšèªã§ãã§ã«åœ¹å²ã決ãŸã£ãŠããåèªã®ããšããããŸãã
å€æ°ãäœãããšããå€æ°ã宣èšããããšãããŸãã
åããŠå€æ°ã«ããŒã¿ãå
¥ããããšããå€æ°ã®åæåããšãããŸãã
###val ãš val ã®éã
Kotlin ã«ã¯å€æ°ãå®çŸ©ããããã®ããŒã¯ãŒããšããŠãval ãš var ãçšæãããŠããŸãã
val: å代å
¥ã§ããªãåç
§ãä¿æããããã®å€æ° (immutable reference)ã§ããJava ã® final å€æ°ã«çžåœããŸããvalue ã®ç¥ã
var: å代å
¥å¯èœãªå€æ° (mutable reference)ã§ããvariable ã®ç¥ã
fun main() {
val NUM:Int = 1
val Num:Int = 2
val num:String = "3ã§ã"
println(NUM)
println(Num)
println(num)
}
#ããŒã¿å
ããŒã¿åãšã¯ãå€æ°ã«å
¥ããããŒã¿ã®çš®é¡ã®ããšã§ãã
ä»åã¯ãããŒã¿åã®æ°å€åãæåååãããŒã«åãããŒã¿åã®åæšè«ã«ã€ããŠèª¬æããŸãã
Kotlinã§ã¯ãå€æ°ã«ããŒã¿ãå
¥ãããšãã«ãããŒã¿åãæå®ããå¿
èŠããããŸãã
âãã®ãããªèšèªã®ããšããéçåä»ãèšèªãšãããKotlinãJavaãGoãCèšèªãªã©ããããŸãã
âéã«ãåçåä»ãèšèªã¯ãããŒã¿åãæå®ããå¿
èŠããªããèªåçã«ããŒã¿åãå€æããŠãããèšèªã§ãã
âRubyãPythonãJavaScriptãPHPãªã©ããããŸãã
###æ°å€å
æ°å€åã«ã¯ãæŽæ°åãšãå°æ°åãããã
æŽæ°åã®ããŒã¿åã¯ãæ¡æ°ãå°ããé ã«Byte,Short,Int,Longãããã
å°æ°åã®ããŒã¿åã¯ãå°æ°ç¹ä»¥äžã®æ¡æ°ãå°ããé ã«Float,DoubleããããŸãã
###æååå
æåååã®ããŒã¿åã¯ãChar
,String
ããããŸãã
Char
ã¯ãã·ã³ã°ã«ã¯ã©ãŒããŒã·ã§ã³ã§ãæå1ã€ã®å Žåã
String
ã¯ãããã«ã¯ã©ãŒããŒã·ã§ã³ã§æå2ã€ä»¥äžã®å Žåã«äœ¿ãåããããŸãã
###ããŒã«å
ããŒã«åã¯ãBooleanåïŒããŒãªã¢ã³åïŒãšããããŸãã
ããŒã«åã¯ãtrueãŸãfalseã®2ã€ã®ãã¡ãã©ã¡ãã1ã€ãæã€åã§ãã
fun main() {
var a: Int = 10
var b: Int = 5
var bool:Boolean;
bool = (a > b);
println(bool);
}
âãtrueãã衚瀺ãããã
###åæšè«
Kotlinã®å Žåãå€æ°ãåæåãããšãã¯ãå€æ°ã«å
¥ããããŒã¿ã®çš®é¡ãæå®ããå¿
èŠããããŸããã
å€æ°ã«ä»£å
¥ããããšããŠããããŒã¿ãããèªåçã«å€æããŠãããããã§ãã
ãããåæšè«ãšèšããŸãã
åæšè«ä»ãéçåä»ãèšèªã¯ãã³ã³ãã€ã«ãããŠããŒã¿åã決ãŸã£ãåŸã¯ãç°ãªãããŒã¿åã®å€ã代å
¥ããããšãã§ããŸããã
ãªããªããåæšè«ä»ãéçåä»ãèšèªã¯ãã³ã³ãã€ã«ããã¿ã€ãã³ã°ã§åã決ããããã§ãã
å€æ°ã®åã調ã¹ãã®ã«ã¯ãjavaClass.kotlin
ã䜿çšããã
fun main() {
val var01 = 1;
val var02 = 12345
val var03 = 1234567890;
val var04 = 123456789000;
println(var01.javaClass.kotlin)
println(var02.javaClass.kotlin)
println(var03.javaClass.kotlin)
println(var04.javaClass.kotlin)
}
####å®è¡çµæ
int (Kotlin reflection is not available)
int (Kotlin reflection is not available)
int (Kotlin reflection is not available)
long (Kotlin reflection is not available)
âintåã§æ±ãããã®ã¯intã«ãæ±ããªããã®ã¯longã«ã
Kotlinãåãèªåã§æšè«ããå€æ°ãåæåããŠãããã
#é
å
é
åãšã¯ãè€æ°ã®ããŒã¿ãæ ŒçŽããããšãã§ããããŒã¿åã§ãã
èŠçŽ
:é
åã®1ã€1ã€ã®ç®±ã®ããš
èŠçŽ æ°
:ç®±ã®æ°ã®ããš
ãã€ã³ããã¯ã¹
:é
åã®ããããã®èŠçŽ ã«å²ãæ¯ãããæ
å ±ã®ããšãäžçªå·ŠããïŒãïŒãïŒ...
###é
åã®å®£èš
é
åã®æžãæ¹ã¯ãããã€ããããŸãããä»åã¯arrayListOf
é¢æ°ã䜿çšããŸãã
äžèšã®ãµã³ãã«ã³ãŒãã®ããŒã¿ãå€æŽããå Žåã«ã¯ãå€æ°å[ã€ã³ããã¯ã¹çªå·] = "å€æŽãããåå"
ãšããã
ãŸããJavaåæ§ã«å€æ¬¡å
é
åãäœãããšãå¯èœã§ãã
fun main() {
val name = arrayListOf("sato","suzuki","tanaka")
println(name[0])
}
#æ¡ä»¶åå²
æ¡ä»¶åå²ãšã¯ãæ¡ä»¶ã«åèŽããå Žåã¯ãåŠçAãããããããªããšãã¯ãåŠçBããšããããšãã§ããŸãã
###ifæ
æ§æ
if(æ¡ä»¶åŒ){æ¡ä»¶ãtrueã ã£ãæã®åŠç}
æ³¢æ¬åŒ§ã¯çç¥ããããšãã§ããŸãã
éãæ¡ä»¶ãèšå®ããå Žåã¯ãelseif()
æ¡ä»¶ãæºãããªãå Žåãèšå®ãããå Žåã¯ãelse
ãµã³ãã«ã³ãŒãã§ã¯ã5以äž10æªæºã ã£ãããåæ Œãããã以å€ã¯ãäžåæ Œããšè¡šç€ºãããŸãã
fun main() {
val integer = 7
if(integer >= 5 && integer < 10) {
println("åæ Œ")
} else {
println("äžåæ Œ")
}
}
#ç¹°ãè¿ã
ç¹°ãè¿ããšã¯ã決ãŸã£ãåæ°ãæ¡ä»¶ãæºãããŠãã°ãåãåŠçãå®è¡ããããã°ã©ã æ§é ã§ãã
###foræ
foræã¯ãæ¡ä»¶ãæºãããŠããã°ãåãåŠçãããããç¹°ãè¿ããŸãã
ãããŠãæ¡ä»¶ãæºãããªããªã£ãã¿ã€ãã³ã°ã§ãç¹°ãè¿ããããããŸãã
ãforæã®æ§æ
for (å€æ° in éå§ããå€..çµäºãããå€){
ãç¹°ãè¿ãäžã«å®è¡ããåŠç;
}
for(å€æ°ãin é
åå€æ°å)
ãšããããšã§ãé
åã®èŠçŽ ãã²ãšã€ãã€å€æ°ã«æ ŒçŽãããŠããã
Javaã§èšããšããã®ãæ¡åŒµforæã¿ãããªæ±ãã ãšæããŸãã
###breakæ
breakæã¯ãããæ¡ä»¶ã«ããŠã¯ãŸã£ããšããç¹°ãè¿ãåŠçãçµäºãããããšãã§ããŸãã
###continueæ
contineæã¯ãç¹°ãè¿ãåŠçã§ãããæ¡ä»¶ã«ããŠã¯ãŸã£ããšãã«ãã®åŠçãã¹ããããããå Žåã«äœ¿ããŸãã
###ãµã³ãã«
ä»åã¯ãforæã§1ãã10ãŸã§1ãã€å¢ãããŠããç¹°ãè¿ããèšè¿°ããŸãã
ãããŠãifæã䜿ãã3ã«ãªã£ããã¹ãããã7ã«ãªã£ããforæããããèšè¿°ãããŸãããã
ãŸãããã®åãããããããã«ãã«ãŠã³ã¿å€æ°ã衚瀺ãããã
fun main() {
for (i in 1..10) {
if (i == 3) {
continue
} else if (i == 7) {
break
} else {
println(i)
}
}
âã1,2,4,5,6ãã衚瀺ãããã
#ã¯ã©ã¹ãšã¯ã
ã¯ã©ã¹ãšã¯ããããŒã¿ããšãåŠçãããŸãšãããã®ã«ãªããŸãã
Kotlinã§ã¯ããããŒã¿ãã®ããšãããããã£
ãšããããåŠçãã®ããšãã¡ãœãã
ãšãããŸãã
ããããã£
ã¯ãã¯ã©ã¹å
ã§å®çŸ©ãããå€æ°
ã®ããšã§ãã
ããããã£ã¯ãå€æ°ãšåãããã«ãæ°å€ãæååã代å
¥ããããåç
§ãããããããšãã§ããŸãã
ããããã£ãšå€æ°ã®éãã¯ãã¯ã©ã¹ã®å€ã«ãããã¯ã©ã¹ã®äžã«ãããã®éãã§ãã
ããããã£
ã¯æååã代å
¥ããããåç
§ãããããããšãã§ããã
ã¡ãœãã
ãé¢æ°
ãšåãã§ããããããªãåŠçãããŸãšããŠ1ã€ã«ãããã®ã§ãã
ç°¡åã«ãããšãã¡ãœãã
ã¯ãã¯ã©ã¹å
ã«å®çŸ©ãããé¢æ°ã§ãã
####ã¯ã©ã¹ã®å®çŸ©
ã¯ã©ã¹ãäœãããšãã¯ã©ã¹ãå®çŸ©ãããšããã
ã¯ã©ã¹åã®æåã®æåã¯ã倧æåã«ããã®ãæ
£ç¿ã«ãªã£ãŠããã
ãµã³ãã«ã§ã¯ã
ã¯ã©ã¹åãTestãšããŠãåœèªãšæ°åŠã®åèšãèšç®ããã¡ãœãããå®çŸ©ããŸãã
class Test {
fun total(japanese: Int, math: Int) {
println(japanese + math)
}
}
###ã¯ã©ã¹ã䜿ã
ã¯ã©ã¹ã䜿çšããã«ã¯ãå®çŸ©ããã ãã§ã¯äœ¿ãããšãã§ããŸããã
ã¯ã©ã¹ããäœãããã€ã³ã¹ã¿ã³ãå€æ°ã«ä»£å
¥ããŠãã䜿ããŸãã
ã€ã³ã¹ã¿ã³ã¹ã«ãªã£ãŠåããŠäœ¿ãããšãã§ããŸãã
###ã€ã³ã¹ã¿ã³ã¹
ã€ã³ã¹ã¿ã³ã¹ãšã¯ãå®æ
ãšããæå³ã§ããã¯ã©ã¹ãšããåãããå®éã«äœ¿ãããã¢ãããäœãããšããããŸãã
ãµã³ãã«ã§ã¯ãnameãšããããããã£ãTestã¯ã©ã¹å
ã§å®çŸ©ããã
mainé¢æ°ã§ãa1ãšããå€æ°ã«Testã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã代å
¥ããŠããã
ãããŠãã€ã³ã¹ã¿ã³ã¹åããTestã¯ã©ã¹ã«å®çŸ©ãããŠããtotalã¡ãœãããåŒã³åºããŠãåŒæ°ã«ç¹æ°ãå
¥ããã
ãããŠãTestã¯ã©ã¹ã®ããããã£nameãåŒã³åºããŠãtanakaãã代å
¥ããã
æåŸã«printlnã§è¡šç€ºãããã
class Test {
var name =""
fun total(japanese: Int, math: Int) {
println(japanese + math)
}
}
fun main() {
//ã€ã³ã¹ã¿ã³ã¹å
var a1 = Test()
a1.total(90,60)
a1.name = "tanaka"
println(a1.name)
}
äžèšã®ããã«ãããããã£ã¯ãã€ã³ã¹ã¿ã³ã¹ããšã«ååšããã
ã€ã³ã¹ã¿ã³ã¹ãå®çŸ©ãã床ã«ç°ãªãããããã£ãå®çŸ©ããªããã°ãªããªãã
ãããããã®ããããã£ãã€ã³ã¹ã¿ã³ã¹ããšã«1ã€ãã€å®çŸ©ããŠããããå£æ°ãããã£ãŠããŸããŸãã
ããã解æ¶ããããã«ã³ã³ã¹ãã©ã¯ã¿
ãããã
###ã³ã³ã¹ãã©ã¯ã¿
Kotlinã«ãããŠJavaãšéãã®ã¯ãã³ã³ã¹ãã©ã¯ã¿ãåºå¥ããŠããããšã§ãã
Kotlinã«ã¯ãã©ã€ããªã³ã³ã¹ãã©ã¯ã¿
ãšã»ã«ã³ããªã³ã³ã¹ãã©ã¯ã¿
ããããŸãã
ã³ã³ã¹ãã©ã¯ã¿ãšã¯ãªããžã§ã¯ãçææã«èªåçã«åŒã°ããåŠçã§ãã
ãã®æ§è³ªããã¯ã©ã¹ã®ã¡ã³ãã®åæååŠççã«ãã䜿çšãããŸããã¯ã©ã¹ã«1ã€ä»¥äžã®ã³ã³ã¹ãã©ã¯ã¿ãå¿
ãååšããŸãã
ä»åã¯ããã©ã€ããªã³ã³ã¹ãã©ã¯ã¿
ã«ã€ããŠèŠãŠãããŸãã
ã³ã³ã¹ãã©ã¯ã¿ã®å®çŸ©
class ã¯ã©ã¹å constructor(åŒæ°) {
}
ãconstructorãã¯ãåºæ¬çã«ã¯çç¥å¯èœã§ãã
以äžã®ãµã³ãã«ã§ã¯ãmanã¯ã©ã¹ã宣èšããŠããŸãã
class man(val fullname: String, val age: Int)
ãã®ã¯ã©ã¹ã«ã¯ããã®ã¯ã©ã¹ã«ã¯æ³¢æ¬åŒ§ãªãã§ãäžžæ¬åŒ§ã«å®£èšãããã ããšãªã£ãŠããŸãã
ãã®ãããªäžžæ¬åŒ§ã§å²ãŸããã³ãŒããããã¯ã¯ããã©ã€ããªã³ã³ã¹ãã©ã¯ã¿ ãšåŒã°ããŸãã
ãã©ã€ããªã³ã³ã¹ãã©ã¯ã¿
ã¯2ã€ã®åœ¹å²ãæã¡ãŸãã
1,ã³ã³ã¹ãã©ã¯ã¿ã®åŒæ°ãæå®ãã
2,åŒæ°ã§åæåãããããããã£ãå®çŸ©ãã
ã³ã³ã¹ãã©ã¯ã©ã¿ã«ãã以äžã®ããã«ã€ã³ã¹ã¿ã³ã¹ãçæã§ããŸã
val man = Man("Yamada Taro",23)
åè
https://www.youtube.com/watch?v=UswtcJlDZ3I
https://www.kenschool.jp/blog/?p=5634