Kotlin: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 14: Line 14:


=Classes=
=Classes=
==General==
<syntaxhighlight lang="kotlin">
<syntaxhighlight lang="kotlin">
class Person {
class Person() {
     var Name: String = ""
     var Name: String = ""
    fun display() {
        println("Display :$Name"
    }
}
}
</syntaxhighlight>
</syntaxhighlight>
==Constuctors==
If you pass a var to a constructor the name is then associated with the class without declaring the value separately
<syntaxhighlight lang="kotlin">
class Person(var Name: String) {
    fun display() {
        println("Display :$Name"
    }
}
</syntaxhighlight>
==Class Functions==
Kotlin supports lambda functions
<syntaxhighlight lang="kotlin">
class Person(var Name: String) {
    fun display() {
        println("Display :$Name"
    }
 
    fun displayWithLambda(func: (s:String) -> Unit) {
        func(Name)
    }
}
</syntaxhighlight>


==Data Class==
==Data Class==

Revision as of 05:09, 22 August 2020

Introduction

Kotlin is like java

  • JVM language
  • Object orientated
  • Functional language, high order functions, we can
  • store, pass and return functions


fun main(args: Array<String>) {
    println("Hello World")
}

Classes

General

class Person() {
    var Name: String = ""

    fun display() {
        println("Display :$Name"
    }
}

Constuctors

If you pass a var to a constructor the name is then associated with the class without declaring the value separately

class Person(var Name: String) {

    fun display() {
        println("Display :$Name"
    }
}

Class Functions

Kotlin supports lambda functions

class Person(var Name: String) {

    fun display() {
        println("Display :$Name"
    }
  
    fun displayWithLambda(func: (s:String) -> Unit) {
        func(Name)
    }
}


Data Class

data class SportsActivity (
       val totalAveragePaceInMinutesPerKilometre: Double,
       val totalAverageSpeedInKilometresPerHour: Double,
       val totalDurationInSeconds: Int,
       val totalAverageDistanceInMetres: Double, 
       var dateOfActivity: Date
)

Reading and Writing to Gson

Given the following

    var mySportsActivity = SportsActivity(
           0.0,
           0.0,
           0,
           0.0,
           Date())
   val gson = Gson()
   val json = gson.toJson(mySportsActivity)
   var filename = "D:\\IAIN\\Output.json";

You can write to a file with

    FileWriter(filename).use {
       writer ->
       val gson = GsonBuilder().setPrettyPrinting().create()
       gson.toJson(mySportsActivity, writer)
    }

And read it back with

    FileReader("D:\\IAIN\\Output.json").use {
       reader ->
       val gson = GsonBuilder().setPrettyPrinting().create()
       mySportsActivity2 = gson.fromJson(reader,SportsActivity::class.java)
    }

Range For Loop

val myTest = 212
for(i in 0..7)
{
  val myGetValue = IsByteSet(myTest,i)
  val myTest2 = myGetValue
}
Reversed is a bit rubbish but here it is
val myTest = 212
for(i in 7 downTo 0)
{
  val myGetValue = IsByteSet(myTest,i)
  val myTest2 = myGetValue
}