본문 바로가기

# 02/코틀린

[Kotlin] 유용한 kotiln Standard 라이브러리 let- data의 null 체크를 할 때 주로 사용 val name : String? = ""name?.let {println("name $it length ${it.length}")} -> 예제fun test() {val name : String? = ""name?.let {println("name $it length ${it.length}") } ?: let {println("null")}} Calls the specified function [block] with this value as its argument and returns its result. public inline fun T.let(block : (T) -> R) : R = block(this) apply- 생성과 동시에 값을 초기화..
[Kotlin] Higher-Order Functions Higher-Order Functions - 함수를 파라미터로 전달하거나, 함수를 리턴 가능- lambda를 통해 축약 형태로 제공- 변수로 함수를 가질 수 있다. Higher-Order Functions 샘플 fun lock(lock : Lock, body : () -> T) : T {lock.lock()try {return body()}finally {lock.unlock()]} /* fun body() : T {return T} */ - 파라미터 이름 : body- 정의 : () -> T Higher-Order Functions 사용하기 fun toBeSynchronized() = sharedResource.operation()val result = lock(lock, ::toBeSynchroniz..
[Kotlin] Class etc Data class - java public final class UserInfo {private String name;private int age; public UserInfo(String name, int age) {this.name = name;this.age = age;} public String getName() {return name;} public void setName(String name) {this.name = name;} public int getAge() {return age;} public void setAge(int age) {this.age = age;} public String toString() {return "UserInfo(name = " +this.name + ", a..
[Kotlin] Sealed Classes 다형성이란? - 부모 클래스를 상속받은 자식 클래스 - 자식 클래스는 부모 클래스로부터 접근할 수 있다. - 이때 자식 클래스는 모두 다른 행동을 할 수 있다. 다형성 - java class A {public void printA() {System.out.println("class A");}} class B extends A {@Overridepublic void printA() {super.printA(); // A 클래스의 printA 출력System.out.println("class B - printA");}} class C extends A {@Overridepublic void printA() {super.printA(); // A 클래스의 printA 출력System.out.println("c..
[Kotlin] Companion Object Companion Object - class 내에 정의할 수 있음- Java에서처럼 Class.TYPE 형태로 직접 접근 가능- static은 아님 -> class 내에 직접 접근을 위한 함수와 변수 정의class Sample {val name : String = "Name" companion object {val type : Int = 0 fun isTypeZero() : Boolean {return type == 0}}}자바에서는 Sample.companion.getType(), Sample.companion.isTypeZero() 로 접근 할 수 있다.코틀린에서는 Sample.type, Sample.isTypeZero() 로 직접 접근할 수 있다. interface와 함께 사용하기 interface..
[Kotlin] Class initializer java - constructor (생성자) public class UserInfo {private String name;private int age;private String birthday; public UserInfo(String name) {this.name = name;} public UserInfo(String name, int age) {this(name);this.age = age;} public UserInfo(String name, int age, String birthday) {this(name, age);this.birthday = birthday;}} kotlin - constructor (생성자) class UserInfo {var name : String = ""var age ..
[Kotlin] Class Inheritance open class 상속 open class Empty class Sample : Empty() java View class 상속 /*open*/ class CustomConstraintLayout (context : Context,attrs : AttributeSet?,defstyleAttr : Int): ConstraintLayout(context, attrs, defStyleAttr) { constructor(context : Context, attrs : AttributeSet?) : this (context, attrs, 0) constructor(context : Context) : this (context, null)} - default 값을 추가하여 secondary 생성자를 제거한다. /..
[Kotlin] Null 예외처리 - etc 안전한 형 변환 - java Object object = "name";int index = (int) object;System.out.println("index " + index); -> java.lang.ClassCastException 발생 - instanceof 추가하여 안전하게 처리 Object object = "name";int index = 0;if( object instanceof Integer) { // object 값이 Integer인지 확인하고 맞으면 변환하므로 안전한 방법임!!index = (int) object;}System.out.println("index " + index); 코틀린 안전한 형변환 시도하기 val a : Any? = "ABC"val b : Int? = a as I..