Class constructor example:
// Class with no body class Car // Class with parameters class Car(var make: String, var model: String, var year: Int, var color: String) { fun drive() { } } Object instantiation: var car = Car("Tesla", "Model S", 2018, "silver") Accessing class properties(local variables) and functions: // property 'make' println(car.make) // prints "Tesla" // function drive() car.drive()
Creating an array of objects: var manyCars = arrayOf(Car("Toyota", "Camry", 2017, "red"), Car("Nissan", "Rogue", 2013, "gray"), Car("Ford", "F-150", 2012, "black")) Accessing an object array's element: manyCars[0].model // "Camry" manyCars[2].drive() // Ford F-150 calls drive() Class initializer blocks: class Car(var make: String, var model: String, var year: Int, var color: String) { fun drive() { } init { print("New car, $make $model pulling up") } init { // this runs after the above 'init'. 'init' blocks run in the order of appearance in code } } Custom getter and setter functions: class Car(var make: String, var model: String, var year: Int, var color: String) { var mpg: Int get() = // do something to calculate/derive the above 'mpg' // a future access of 'Car.mpg' would auto-call this get() var verifiedYear = year set(value) { if(value != 0) { // field identifier used Kotlin to avoid circular dependency issues field = value } } set() // often used to validate values/data // set() auto-called each time a value is assigned e.g. 'Car.verifiedYear = 1999' fun drive() { } } - If there is no explicit getter or setter declarations, the compiler creates them automatically. 'val' properties have just the getter method (due to value being immutable) created, while 'var' properties get both. Superclasses and Subclasses
// Superclass open class Car(var make: String, var model: String, var year: Int, var color: String) { var mpg open val country open fun drive() { } } // Subclass class SportsCar : Car() { // use override keyword to override superclass properties or functions override val country override fun drive() // for superclass properties defined with 'var', it doesn't need to be overridden init { mpg = 25 } } Abstract classes & interfaces Abstract classes and interfaces are still declared the same way in Kotlin using the 'abstract' and 'interface' keywords. // abstract class abstract class Animal { abstract val species: String fun classify() { ... } } // interface interface Hunt { fun chase() { println("Chase prey") } } Kotlin does not have the keywords 'inherits' or 'implements'. // The class MyAnimal implements the 'Hunt' interface and inherits from the 'Animal' class class MyAnimal: Hunt, Animal() { override val species: String get() = "Lion" } Abstract class constructor abstract class Animal(private val species: String) { fun classify() { println("The animal is part of the $species species") } }
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorExploring Android and mobile web design, security, and development. Archives
March 2021
Categories |