kotlin

Kotlin class-02

slow333 2023. 1. 14. 11:55

12. 기본 프로젝트 구조

    프로젝트 > module(구현 또는 라이브러리에서 가져옴) > 폴더 > 파일

package 이름 com.bloomingsystem.base

 

package com.bloomingsystem.base

import com.ac.net.ai

Main () {

}

    import 패키지와 class 이름이 중복되면 패키지 이름을 포함해서 사용

 

13. 변수, 함수, class 접근범위/접근제한자

    패키지 안에 변수, 함수, class 있으면 하나의 scope안에 있는

·    아무 것도 안적으면 default package 포함됨

·    기본 적으로 scope 안에서는 변수를 사용 가능

-------------------------------------------------

var a : String = "패키지 scope"

 

class B {

    val a = "class Scope"

    fun print(){

        println(a)

    }

}

 

fun main() {

    val a = "fun scope"

    println(a)

    B().print()

}

 

    하위 scope에서는 상위 scope 정의해서 사용가능

·    같은 scope 내에서는 같은 변수를 사용하면 안됨

·    scope 외부에서 다른 scope 접근하고 참조 연산자를 사용

    access modifier: scope 외부에서 다른 scope 접근할 개발자가 제거하는 기능

·    public, internal, private, protected 있음

    변수, 함수, class 선언시 맨앞에 붙여서 사용

private var a = 55

public fun b {…}

internal class C {…}

 

패키지 scope에서는

    public(기본값) : 어떤 패키지에서도 접근 가능

    internal : 같은 모듈 내에서만 접근 가능

    private : 같은 파일 내에서만 접근 가능

    protected 패키지 scope에서 사용하지 않음

class scope에서는

    public(기본값) : class 외부에서 접근 가능

    private : class 내에서만 접근 가능

    protected : class 자신과 상속받은 class에서 접근 가능

    internal class scope에서 사용하지 않음

 

14. 고차함수와 람다함수

    고차함수는 함수를 마치 class에서 만들어 ‘instance’ 처럼 취급하는 방법

-------------------------------------------------

fun main() {

    b(::a)

//     val c : (String) -> Unit = {str : String -> println("$str 람다함수")}

    val c = {str : String -> println("$str 람다함수")}

// (String) -> Unit 자료형으로 저장됨

    b(c)

}

 

fun a (str:String){

    println("$str 함수 .../")

}

 

fun b (function: (String) -> Unit){

    function("b 호출한   ___ ")

}

-------------------------------------------------

fun main() {

// 여러줄을 사용하는 람다함수

    val c : (Int, Int) -> Int = { a, b ->

        println(a)

        println(b)

        a + b

    }

    println(c(3,4))

    val d : () -> Unit = {println("파라미터 없는 람다")}

    d()

 

 //파라미터가 하나면 it 사용

    val e : (String) -> Unit = {println("${it} 람다함수")}

    e("it 사용")

}

 

 

// lamda 함수

// val lamdaName : Type = {argumentList -> codeBody}

 

val square = { number : Int -> number*number}

 

val nameAge = {name: String, age : Int ->

    println("My name is ${name} and ${age} years old.")

}

 

// 확장 함수

 

val pizza : String.() -> String = {

    this + " Pizza is better"

}

 

fun weightAndAge(weight : Int, age : Int) : String {

    val myself : Int.(Int) -> String = {"My weight is ${this} and ${it} years old"}

    return weight.myself(age)

}

 

// lambda return

val calculateGrade : (Int) -> String = {

    when(it) {

        in 0..30 -> "You failed."

        in 31..60 -> "C"

        in 61..80 -> "Not bad"

        in 81..100 -> "Excellent"

        else -> "error input correct points"

    }

}

 

//lambda 표현하는 여러가지 방법

 

fun invokeLambda(lambda : (Double) -> Boolean) : Boolean {

    return lambda(5.2334)

}

 

fun main() {

    println(square(12))

    nameAge("kim", 44)

    val a = "Me said"

    val b = "You said"

   

    println(a.pizza())

    println(b.pizza())

    println(weightAndAge(88, 55))

    println(calculateGrade(333))

   

    val lam : (Double) -> Boolean = {number : Double ->

        number == 5.233

    }

    println(invokeLambda(lam))

    println(invokeLambda{it > 3.32})

}

 

 

'kotlin' 카테고리의 다른 글

Kotlin String  (0) 2023.01.14
Kotlin List  (0) 2023.01.14
Kotlin class-03; Object, companion object, Generic  (0) 2023.01.14
Kotlin class-01  (0) 2023.01.14
Kotlin 기본 형, loop ,if  (0) 2023.01.14