name: portada class: portada-slide, center, middle # Kotlin ## Poo <%= teacher_slides_start %> --- # Classe ```kotlin class Car(val model:String, val price: Double? = null) ``` --- # Atributs ```kotlin // Atributs definits a la constructora (var o val) class Rectangle(var height:Double, var width:Double){ // Més atributs val createdAt = Instant.now() // Atribut calculats val area get() = height * width } fun main(){ val rectangle = Rectangle(2.0,4.0) //Accés als atributs println(rectangle.width) println(rectangle.createdAt) println(rectangle.area) } ``` --- # Init ```kotlin class Sample1(val attribute1: Int, notAnAttribute: Int){ val attribute2 : Int init { attribute2 = notAnAttribute } } ``` ``` val sample = Sample1(1,2) println(sample.attribute1) // println(sample.notAnAttribute) <- Invalid println(sample.attribute2) ``` --- # Init ``` class Size(width: Int, height: Int) { init { // assert assegura que es complexi la condició, // sinó llança una excepció assert(width < 0) {"Width can't be negative"} assert(height < 0) {"height can't be negative"} } } ``` --- # Constructors secundari ```kotlin data class Car( val model: String, val price: Double? = null, val old: Boolean ){ constructor( model: String, price: Double? = null, year: Int ) : this(model, price, year<2000) } ``` --- # Visibilitat - For members declared inside a class: - __private__ El membres són visibles només en aquesta classe. - __protected__ Els membres són visibles per aquesta classe i subclasses. - __internal__ Els membres són visibles des de dins del mòdul. - __public__ els membres són accessibles des de fora de la classe. --- # Visibilitat ``` class SomeClass { private val privateVal = 1 protected val protectedVal = 2 internal val internalVal = 3 val publicVal = 4 // public by default protected fun privateMethod() {} fun publicMethod() {} // public by default } fun main() { val instance = SomeClass() // instance.privateVal <- No visible // instance.protectedVal <- No visible instance.publicVal instance.internalVal // instance.privateMethod() <- No visible instance.publicMethod() } ``` --- # Default and named arguments ``` class Size(var width: Int = 1, var height: Int = 1) { val area get() = height * width } ``` ``` val size1 = Size(3, 5) // width == 3, height == 5 val size2 = Size(width = 3, height = 5) // width == 3, height == 5 val size3 = Size(height = 5, width = 3) // width == 3, height == 5 val sizeWide = Size(10) // width == 10, height == 1 poc llegible! val sizeHigh = Size(height = 10) // width == 1, height == 10 ``` --- # Enum ```kotlin enum class DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } ``` ```kotlin val dow = DayOfWeek.MONDAY ``` --- # Enum ```kotlin fun tellItLikeItIs() = when (dow) { MONDAY -> "Mondays are bad." TUESDAY, WEDNESDAY, THURSDAY -> "Midweek days are so-so." FRIDAY -> "Fridays are better." SATURDAY, SUNDAY -> "Weekends are best." } ``` --- Enumeracions: https://fp.mateuyabar.com/DAM-M03/UF4/02_enums.html Herència: https://fp.mateuyabar.com/DAM-M03/UF4/03_inheritance.html Classes abstractes i interfícies: https://fp.mateuyabar.com/DAM-M03/UF4/04_classes_abstractes.html Introducció a generics: https://fp.mateuyabar.com/DAM-M03/UF4/91_introduccio_a_generics.html Delegate: https://fp.mateuyabar.com/DAM-M03/UF4/92_delegation.html --- # Exercicis - https://fp.mateuyabar.com/DAM-M03/UF4/exercicis/01_exercicis.html - exercicis: 2, 3, 4 - [mostpopulatedcountries](https://fp.mateuyabar.com/DAM-M03/UF1/exercicis/06_list.html#mostpopulatedcountries)