๐Ÿ“ฆ Chango/๐Ÿฃ EDOC

[AndroidStudio / Kotlin] ์ฝ”ํ‹€๋ฆฐ ๊ธฐ๋ณธ๋ฌธ๋ฒ•

์„ ๋‹ฌ 2021. 4. 30. 16:03
๋ฐ˜์‘ํ˜•

#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}")
}

 

๋ฐ˜์‘ํ˜•