๋ฐ์ํ
#AndroidStudioAndroidStudio ํ๊ทธ ์ญ์ #KotlinKotlin ํ๊ทธ ์ญ์ #AppDevAppDev ํ๊ทธ ์ญ์ ๊ณต๋ถํ๊ณณ : www.youtube.com/watch?v=IDVnZPjRCYg
1. ํจ์ function
//1. function
fun helloWorld() {
println("Hello World!")
}
fun add(a : Int, b : Int) : Int {
return a+b
}
2. val vs var
//2. val vs var
// val = value
fun hi(){
val a: Int = 10
var b: Int = 9
var e : String
b = 100
val c = 100
val d = 100
var name : String = "whkakrkr"
}
3. String Template
//3. String Template
val name = "whk"
val lastname = "akrkr"
println("my name is ${name + lastname}I'm 21")
println("I want 150\$")
4. ์กฐ๊ฑด์
//4. ์กฐ๊ฑด์
fun maxBy(a: Int, b: Int) : Int {
if( a>b ) {
return a
} else {
return b
}
}
fun maxBy2(a: Int, b: Int) : Int = if(a>b) a else b // return(a>b) ? a:b
fun checkNum(score : Int) {
when(score) {
0 -> println("this is 0")
1 -> println("this is 1")
2,3 -> println("this is 2 or 3")
else -> println("I don't know") //์ ํ
}
var b : Int = when(score){
1-> 1
2-> 2
else -> 3 //ํ์์
}
println("b : ${b}")
when(score){
in 90..10 -> println ("You are genius")
in 10..80 -> println("not bad")
else -> println("okay") //์ ํ์
}
}
** Expression vs Statement
- Expression : ๋ฌด์ธ๊ฐ๋ฅผ ํด์ ๊ฐ์ ๋ง๋ฌ -> kotlin์ ๋ชจ๋ ํจ์๋ expression
- Statement : ๊ฐ์ ๋ง๋ค์ง๋ ์์ (ex. java์ void ํจ์)
5. Array and List
-Array : ํฌ๊ธฐ๊ฐ ์์
-List : 1) List, 2) MutableList
// 5. Array and List
fun array(){
val array : Array<Int> = arrayOf(1,2,3)
val list : List<Int> = listOf(1,2,3)
val array2 : Array<Any> = arrayOf(1, "d", 3.4f)
val list2 : List<Any> = listOf(1,"d", 3.4f)
array[0] = 3
var result : Int = list.get(0)
var arrayList : ArrayList<Int> = arrayListOf<Int>()
arrayList.add(10)
arrayList.add(20) //listA listB
}
6. ๋ฐ๋ณต๋ฌธ
for( i:Int in 1..10){
sum += i //55
}
for( i:Int in 1..10 step 2 ){
sum += i //25
}
for( i:Int in 10 downTo 1 ){
sum += i // 55
}
for(i:Int in 1 until 10){
sum += i //45
}
val student : ArrayList<String> = arrayListOf("whk","ak","rkr")
for ( name : String in student ){
println("${name}")
}
for((index:Int, name:String) in student.withIndex()){
println("${index+1}๋ฒ์งธ ํ์ : ${name}")
}
var index =0
while (index < 10){
println("current index : ${index}")
index++
}
7. nullable / nonnull
fun nullcheck(){
//NPE : Null pointer Exception
var name : String = "whk"
var nullName : String? = null
var nameInUpperCase : String = name.toUpperCase()
var nullNameInUpperCase : String? = nullName?.toUpperCase()
// ? :
val lastname : String = "akrkr"
val fullName : String = name + " " + (lastname ?: "No lastName")
println(fullName)
// !!
}
fun ignoreNulls(str : String?){
val mNotNull : String = str!!
val upper : String = mNotNull.toUpperCase()
val email : String? = "whkakrkr@gmail.com"
email?.let{
println("my email is ${email}")
}
}
8. Class
open class Human( val name : String = "Anonymous") {
constructor(name : String, age : Int) : this(name) {
println("my name is ${name}, ${age}years old")
}
init { //์ฃผ์์ฑ
println("New human has been born!!")
}
fun eatingCake() {
println("This is so yummy~~~")
}
open fun singASong() {
println("lalalalala")
}
}
class Korean : Human(){
override fun singASong(){
super.singASong()
println("๋๋๋ผ")
println("my name is ${name}")
}
}
fun main(){
// val human = Human("akrkr")
//
// val stranger = Human()
//
// human.eatingCake()
// val mom = Human("yoon",51)
val korean = Korean()
korean.singASong()
// println("this human's name is ${stranger.name}")
}
๋ฐ์ํ
'๐ฆ Chango > ๐ฃ EDOC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android Studio / Kotlin] ์ฝํ๋ฆฐ๊ณผ ๋ฆฌ์คํธ๋ทฐ (List View) (0) | 2021.05.02 |
---|---|
[Android Studio / Kotlin] ์ค์ ์ฑ๊ฐ๋ฐ์์ ์ฌ์ฉํ ์ฝํ๋ฆฐ๋ง ๋ชจ์๋ณด๊ธฐ (๊ธฐ๋ณธ) (0) | 2021.05.02 |
[AndroidStudio / Kotlin] Unresolved reference ์ค๋ฅ ํด๊ฒฐ (0) | 2021.05.01 |
[AndroidStudio / Kotlin] ์ฝํ๋ฆฐ ๊ณ ๊ธ๋ฌธ๋ฒ (0) | 2021.04.30 |
[CSS] Flexbox Froggy ํ๋ฉด์ CSS flexbox ๊ณต๋ถํ๊ธฐ (0) | 2021.04.29 |