Android/Kotlin
[Kotlin] 클래스 toString/equals/hashCode, data class
클래스 - toString val product = Product("패션", "운동화") println("$product") // Product@60addb54 -> 인스턴스 정보를 얻는데 도움X class Product(val categoryLabel: String, val name: String=""){ override fun toString(): String { return "Product(categoryLabel=$categoryLabel, name=$name)" } } 클래스 - equals val prodcut1 = Product("패션", "운동화") val prodcut1 = Product("패션", "운동화") println(product1 == product2) // false - 같은..
[Kotlin] 함수, Superclass, Subclass
함수 1) 함수의 body 가 block // 반환 타입 없음 fun start(){ println("Hello World!") } // 반환 타입 있음 fun validString(string: String?): Boolean { return !string.isNullOrBlank() } 2) 함수 body가 식 (Single expression) fun double(x: Int): Int = x * 2 // 반환 타입 생략 가능 fun double(x: Int) = x * 2 3) top-level 함수 - 클래스 안에 함수를 넣지 않아도 됨 - main 함수 4) 클래스 멤버 함수 - 클래스 내부에 정의 fun main(args: Array){ val home = Home() home.start()..
[Kotlin] 클래스 상속, 추상화, 인터페이스
객체 - 객체는 식별자(identity), 상태(state), 행동(behavior) 을 가진 실체 클래스 class Product // 필수 파라미터 지정 class Product constructor(val categoryLabe: String) // constructor 생성자가 한 개 있을 시 생략 가능 class Product(val categoryLabel: String) // constructor 생성자가 두 개 이상 일 시 (주 생성자, 부 생성자) class Product(val categoryLabel: String) { constructor(categoryLabel: String, name: String): this(categoryLabel) } // 파라미터에 디폴트 값이 존재한다면..
[Kotlin] Operator, conditions & loop
Operator - Safe calls ?. : safe call operator - nullable 타입 변수 속성에 접근 가능 var userName: String? = null if(userName.isEmpty() // Error : userName이 null이므로 isEmpty() 호출 불가능 { println("사용자 이름을 다시 입력해주세요.") } if(userName?.isEmpty() == true) { println("사용자 이름을 다시 입력해주세요.") } if(userName?.isNullOrEmpty()) // 공백을 여러 번 입력하는 경우 처리가 되지 않음 println("사용자 이름을 다시 입력해주세요.") } if(userName?.isNullOrBlank()) // 빈 문..
[Kotlin] Variables & Types
변수 - val : 변경 불가능한 변수 immutable - var : 변경 가능한 변수 mutable val appName: String = "Shoppi" // Type 지정 val userName = "Bob" // Type 미지정 Integer types - Byte - Short - Int - Long val one = 1 // Int val twoBillion = 2000000000 // Long val oneLong = 1L //Long val oneByte: Byte = 1 Floating-point types - Float (Decimal digits : 6-7) - Double (Decimal digits : 15-16) val pi = 3.14 // Double val oneDoubl..